You could use %g to achieve this:
'%g'%(3.140)
or, with Python ≥ 2.6:
'{0:g}'.format(3.140)
or, with Python ≥ 3.6:
f'{3.140:g}'
From the docs for format: g causes (among other things)
Answer from unutbu on Stack Overflowinsignificant trailing zeros [to be] removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
You could use %g to achieve this:
'%g'%(3.140)
or, with Python ≥ 2.6:
'{0:g}'.format(3.140)
or, with Python ≥ 3.6:
f'{3.140:g}'
From the docs for format: g causes (among other things)
insignificant trailing zeros [to be] removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
Me, I'd do ('%f' % x).rstrip('0').rstrip('.') -- guarantees fixed-point formatting rather than scientific notation, etc etc. Yeah, not as slick and elegant as %g, but, it works (and I don't know how to force %g to never use scientific notation;-).
python - dropping trailing '.0' from floats - Stack Overflow
keep trailing 0's in floats?
python - How to format a float with a maximum number of decimal places and without extra zero padding? - Stack Overflow
python - How to print float to n decimal places including trailing 0s? - Stack Overflow
See PEP 3101:
'g' - General format. This prints the number as a fixed-point
number, unless the number is too large, in which case
it switches to 'e' exponent notation.
Old style (not preferred):
>>> "%g" % float(10)
'10'
New style:
>>> '{0:g}'.format(float(21))
'21'
New style 3.6+:
>>> f'{float(21):g}'
'21'
rstrip doesn't do what you want it to do, it strips any of the characters you give it and not a suffix:
>>> '30000.0'.rstrip('.0')
'3'
Actually, just '%g' % i will do what you want.
EDIT: as Robert pointed out in his comment this won't work for large numbers since it uses the default precision of %g which is 6 significant digits.
Since str(i) uses 12 significant digits, I think this will work:
>>> numbers = [ 0.0, 1.0, 0.1, 123456.7 ]
>>> ['%.12g' % n for n in numbers]
['1', '0', '0.1', '123456.7']
trying to make a money calculator, so I want to keep 0's
10.10 instead of 10.1
do I have to converting to a string for printing and use {2:g} or can I get floats to show 0's?
EDIT
solved,
format(number, '.2f' )
unlesss thers a better way?
What you're asking for should be addressed by rounding methods like the built-in round function. Then let the float number be naturally displayed with its string representation.
>>> round(65.53, 4) # num decimal <= precision, do nothing
'65.53'
>>> round(40.355435, 4) # num decimal > precision, round
'40.3554'
>>> round(0, 4) # note: converts int to float
'0.0'
Sorry, the best I can do:
' {:0.4f}'.format(1./2.).rstrip('0')
Corrected:
ff=1./2.
' {:0.4f}'.format(ff).rstrip('0')+'0'[0:(ff%1==0)]
For Python versions in 2.6+ and 3.x
You can use the str.format method. Examples:
>>> print('{0:.16f}'.format(1.6))
1.6000000000000001
>>> print('{0:.15f}'.format(1.6))
1.600000000000000
Note the 1 at the end of the first example is rounding error; it happens because exact representation of the decimal number 1.6 requires an infinite number binary digits. Since floating-point numbers have a finite number of bits, the number is rounded to a nearby, but not equal, value.
For Python versions prior to 2.6 (at least back to 2.0)
You can use the "modulo-formatting" syntax (this works for Python 2.6 and 2.7 too):
>>> print '%.16f' % 1.6
1.6000000000000001
>>> print '%.15f' % 1.6
1.600000000000000
The cleanest way in modern Python >=3.6, is to use an f-string with string formatting:
>>> var = 1.6
>>> f"{var:.16f}"
'1.6000000000000001'
If you want to "avoid" the last 1, which occurs at the 15th decimal place because of how floating point numbers work, you can convert the float first into a string representation and then into a Decimal:
>>> from decimal import Decimal
>>> f"{Decimal(repr(var)):.16f}"
'1.6000000000000000'
Note that if you are working with numbers that need 15 decimal places of precision, you should not be using floats in the first place but should build your solution around Decimals from the get-go.