WHat is a good way to format a python decimal like this way?
1.00 --> '1'
1.20 --> '1.2'
1.23 --> '1.23'
1.234 --> '1.23'
1.2345 --> '1.23'
Use str. format() with “{:. 2f}” as string and float as a number to display 2 decimal places in Python.
Answer. In Python, string formatters are essentially placeholders that let us pass in different values into some formatted string. The %d formatter is used to input decimal values, or whole numbers. If you provide a float value, it will convert it to a whole number, by truncating the values after the decimal point.
If you have Python 2.6 or newer, use format
:
'{0:.3g}'.format(num)
For Python 2.5 or older:
'%.3g'%(num)
Explanation:
{0}
tells format
to print the first argument -- in this case, num
.
Everything after the colon (:) specifies the format_spec
.
.3
sets the precision to 3.
g
removes insignificant zeros. See http://en.wikipedia.org/wiki/Printf#fprintf
For example:
tests=[(1.00, '1'), (1.2, '1.2'), (1.23, '1.23'), (1.234, '1.23'), (1.2345, '1.23')] for num, answer in tests: result = '{0:.3g}'.format(num) if result != answer: print('Error: {0} --> {1} != {2}'.format(num, result, answer)) exit() else: print('{0} --> {1}'.format(num,result))
yields
1.0 --> 1 1.2 --> 1.2 1.23 --> 1.23 1.234 --> 1.23 1.2345 --> 1.23
Using Python 3.6 or newer, you could use f-strings
:
In [40]: num = 1.234; f'{num:.3g}' Out[40]: '1.23'
Only first part of Justin's answer is correct. Using "%.3g" will not work for all cases as .3 is not the precision, but total number of digits. Try it for numbers like 1000.123 and it breaks.
So, I would use what Justin is suggesting:
>>> ('%.4f' % 12340.123456).rstrip('0').rstrip('.') '12340.1235' >>> ('%.4f' % -400).rstrip('0').rstrip('.') '-400' >>> ('%.4f' % 0).rstrip('0').rstrip('.') '0' >>> ('%.4f' % .1).rstrip('0').rstrip('.') '0.1'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With