tags:

views:

1814

answers:

3

I have a problem which prints out a float variable. What I should get is for example: 104.0625, 119.0, 72.0. I know how to control the number of decimals but how do I control the number of zeros specifically, i.e. when the float is 104.06250000 the program should print 104.0625 but when the float is 119.00000 or 72.000000 etc., I should have 119.0 and 72.0.

+1  A: 

The '%2.2f' operator will only do the same number of decimals regardless of how many significant digits the number has. You will have to identify this in the number and manually frig the format. You might be able to short-cut this by printing to a string with a large number of decimal places and strip all of the trailing zeros.

A trivial function to do this might look something like intel_format() in the sample below:

import re

foo_string = '%.10f' % (1.33333)
bar_string = '%.10f' % (1)

print 'Raw Output'
print foo_string
print bar_string

print 'Strip trailing zeros'
print re.split ('0+$', foo_string)[0]
print re.split ('0+$', bar_string)[0]

print 'Intelligently strip trailing zeros'
def intel_format (nn):
    formatted = '%.10f' % (nn)
    stripped = re.split('0+$', formatted)
    if stripped[0][-1] == '.':
        return stripped[0] + '0'
    else:
        return stripped[0]

print intel_format (1.3333)
print intel_format (1.0)

When run, you get this output:

Raw Output
1.3333300000
1.0000000000
Strip trailing zeros
1.33333
1.
Intelligently strip trailing zeros
1.3333
1.0
ConcernedOfTunbridgeWells
Thank You! I also came up with testing if float_variable % 1 == 0, to do this.
zequzd
+2  A: 

I don't think there is a pre-defined format for this - format in python are inherited from C, and I am pretty sure you don't have the desired format in C.

Now, in python 3, you have the format special function, where you can do your own formatting. Removing the last zeros in python is very easy: just use the strip method:

a = 1.23040000
print str(a).rstrip('0')

If you want to keep a 0 after the decimal point, that's not very difficult either.

David Cournapeau
Ok, great! Now I just have to deal with the situation 1.000000 -> 1.0 separately, any short way of doing that, i.e checking if there are only zeros after "."?
zequzd
I would go for the straightforward answer: detect if the last character is a point, and add a 0 if that's the case. Keep in mind that this won't work under localization (for example in my native language, French, we use ',' for the decimal point and '.' for thousand separator).
David Cournapeau
+2  A: 

How about using the decimal module?

From the documentation:

"The decimal module incorporates a notion of significant places so that 1.30 + 1.20 is 2.50. The trailing zero is kept to indicate significance. This is the customary presentation for monetary applications. For multiplication, the “schoolbook” approach uses all the figures in the multiplicands. For instance, 1.3 * 1.2 gives 1.56 while 1.30 * 1.20 gives 1.5600."

The normalize() function removes trailing zeros:

>>> from decimal import *
>>> d1 = Decimal("1.30")
>>> d2 = Decimal("1.20")
>>> d3
Decimal("1.5600")
>>> d3.normalize()
Decimal("1.56")
Adam Bernier
Yes, this might also. However, the program I had to write was by means scientific in nature or didn't have anything to do with significance, it was rather a trivial excercise, but now it works. Thank You!
zequzd