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'
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'
Just use Python's standard string formatting methods:
>>> "{0:.2}".format(1.234232)
'1.2'
>>> "{0:.3}".format(1.234232)
'1.23'
If you are using a Python version under 2.6, use
>>> "%f" % 1.32423
'1.324230'
>>> "%.2f" % 1.32423
'1.32'
>>> "%d" % 1.32423
'1'
Here's a function that will do the trick:
def myformat(x):
return ('%.2f' % x).rstrip('0').rstrip('.')
And here are your examples:
>>> myformat(1.00)
'1'
>>> myformat(1.20)
'1.2'
>>> myformat(1.23)
'1.23'
>>> myformat(1.234)
'1.23'
>>> myformat(1.2345)
'1.23'
Edit:
From looking at other people's answers and experimenting, I found that g does all of the stripping stuff for you. So,
'%.3g' % x
works splendidly too and is slightly different from what other people are suggesting (using '{0:.3}'.format() stuff). I guess take your pick.
Step 1: round the number, or simply trim it to two decimal places.
Step 2: convert it to a string.
Step 3: remove the trailing zero that it could have (if it were '1.0' or '1.20', for instance).
Step 4: remove the trailing decimal point that it could have (if it were '1.' after removing a trailing zero, for instance).
Here it is:
import re
float = <whatever>
rounded = str(round(float, 2))
replaceTrailingZero = re.compile('0$')
noTrailingZeros = replaceTrailingZeros.sub('', rounded)
replaceTrailingPeriod = re.compile('\.$')
finalNumber = replaceTrailingPeriod.sub('', noTrailingZeros)
If you have Python 2.6 or better, use format
:
'{0:.3g}'.format(num)
For Python 2.5 or worse:
'%.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