Printing floats with a certain number of zeros

I know how to control the number of decimal places, but how can I control the number of zeros?

For example:

104.06250000 -> 104.0625 119.00000 -> 119.0 72.000000 -> 72.0 
+6
python floating-point
source share
6 answers

I do not think there is a predefined format for this. The format in python is inherited from C, and I'm sure you don't have the format you need in C.

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

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

If you want to keep 0 after the decimal point, this is also not very difficult.

+5
source share

How about using a decimal module?

In the documentation:

β€œThe decimal module includes the notion of significant places, so 1.30 + 1.20 is 2.50. A trailing zero is used to indicate significance. This is a common representation for monetary applications. For multiplication, the school book approach uses all the numbers in the factors. For example, 1.3 * 1.2 gives 1.56 and 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") 
+9
source share

I think the easiest way would be a round function. in your case:

 >>> round(104.06250000,4) 104.0625 
+4
source share

The operator '%2.2f' will only execute the same number of decimal places, regardless of how many significant digits the number has. You will need to determine this number and manually change the format. You could shorten this by printing a line with a lot of decimal places and separating all trailing zeros.

The trivial function for this might look something like intel_format() in the example 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 you start, you get this output:

 Raw Output 1.3333300000 1.0000000000 Strip trailing zeros 1.33333 1. Intelligently strip trailing zeros 1.3333 1.0 
+1
source share

You can use the string method format if you use Python 2.6 and higher.

 >>> 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 
0
source share
 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 
-2
source share

All Articles