zequzd
zequzd

Reputation: 653

Printing floats with a specific number of zeros

I know how to control the number of decimals, but how do I control the number of zeros specifically?

For example:

104.06250000 -> 104.0625   
119.00000 -> 119.0  
72.000000 -> 72.0 

Upvotes: 6

Views: 27856

Answers (6)

pyssling
pyssling

Reputation: 1

You can use the string method format if you're using Python 2.6 and above.

>>> print "{0}".format(1.0)
1.0
>>> print "{0}".format(1.01)
1.01
>>> print "{0}".format(float(1))
1.0
>>> print "{0}".format(1.010000)
1.01

Upvotes: 0

Moj
Moj

Reputation: 6361

I think the simplest way would be "round" function. in your case:

>>> round(104.06250000,4)
104.0625

Upvotes: 4

Yuanzhong Deng
Yuanzhong Deng

Reputation: 218

def float_remove_zeros(input_val):
    """
    Remove the last zeros after the decimal point:
    * 34.020 -> 34.02
    * 34.000 -> 34
    * 0 -> 0
    * 0.0 -> 0
    """
    stripped = input_val
    if input_val != 0:
        stripped = str(input_val).rstrip('0').rstrip('.')
    else:
        stripped = 0
    return stripped

Upvotes: -2

mechanical_meat
mechanical_meat

Reputation: 169494

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")

Upvotes: 9

ConcernedOfTunbridgeWells
ConcernedOfTunbridgeWells

Reputation: 66702

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

Upvotes: 1

David Cournapeau
David Cournapeau

Reputation: 80770

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.

Upvotes: 5

Related Questions