Format it to 6 decimal places:
format(value, '.6f')
Demo:
>>> format(2.0, '.6f')
'2.000000'
The format() function turns values to strings following the formatting instructions given.
Format it to 6 decimal places:
format(value, '.6f')
Demo:
>>> format(2.0, '.6f')
'2.000000'
The format() function turns values to strings following the formatting instructions given.
From Python 3.6 it's also possible to do f-string formatting. This looks like:
f"{value:.6f}"
Example:
> print(f"{2.0:.6f}")
'2.000000'
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?
I'm doing a codeeval problem where I have to sort numbers and KEEP the trailing zeros. Whenever I convert the string to a float, I lose the trailing zero and I've been really stuck on this.
Here is my code: http://pastebin.com/vjzQqRqK
Here's some random numbers I've been working with:
-91.990 33.414 -43.337 95.988 27.428 49.803 -68.933 -49.591 -66.642 35.075 67.539 92.211 -59.036 -11.980 47.774 92.474 33.761 11.232 -81.835 19.262 77.708 -39.278 -0.669 81.392 -93.143 -50.236 -81.395 96.431 -1
Everything works expect I cannot figure out how to keep the trailing zero on numbers such as -91.990
You can use the format method on strings to specify how many decimal places you want to represent:
>>> "{:.2f}".format(1.5)
'1.50'
But even better would be to use the decimal module for representing money, since representation issues with binary floats can give you slightly off results if you're doing arithmetic. The documentation for that module mentions some of those issues specifically - one of the most interesting ones for money applications is:
>>> 0.1+0.1+0.1-0.3
5.551115123125783e-17
>>> from decimal import Decimal
>>> Decimal('.1') + Decimal('.1') + Decimal('.1') - Decimal('.3')
Decimal('0.0')
The proposed solutions do not work when the magnitude of the number is not known in advance, which is common in scientific applications rather than in money-related ones. I give an alternative solution for those, like me, coming to this question looking for the scientific case.
For example, if I want to print x = 1.500e-4 to three significant digits (common situation when dealing with measurements with a given uncertainty), the following command obviously does not give the correct result:
x = 1.500e-4
print(f"{x:.3f}")
----> 0.000
Here I used the modern Python 3.6+ f-strings for the formatting.
One may think of using the g format specifier, but this also does not give the desired result to three significant digits as the trailing zero is omitted:
x = 1.500e-4
print(f"{x:.3g}")
----> 0.00015
The correct answer can be obtained using the g format specifier together with a rather obscure option, the hash character #, of the format-specification mini language, in the following way:
x = 1.500e-4
print(f"{x:#.3g}")
----> 0.000150
This formatting also works unchanged in the simpler case of the original question:
x = 1.500
print(f"{x:#.3g}")
----> 1.50
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.
Multiply by 10 every time you want to add another 0.
This is the same as saying 10 to the power of how many zeroes you want. In python, that would be number * (10**extraZeroCount)
As I understand the question, the 3 extra zeros are for output purposes only. There is no need to obtain integers back. Strings (or for that mater, any other type) is enough. In this vein, any of
print(a*1000)
print(str(a)+"000")
print(a,"000",sep="")
and perhaps several others, would work.