How do I get a decimal value when using the division operator in Python?
Solution 1
There are three options:
>>> 4 / float(100)
0.04
>>> 4 / 100.0
0.04
which is the same behavior as the C, C++, Java etc, or
>>> from __future__ import division
>>> 4 / 100
0.04
You can also activate this behavior by passing the argument -Qnew
to the Python interpreter:
$ python -Qnew
>>> 4 / 100
0.04
The second option will be the default in Python 3.0. If you want to have the old integer division, you have to use the //
operator.
Edit: added section about -Qnew
, thanks to ΤΖΩΤΖΙΟΥ!
Solution 2
Other answers suggest how to get a floating-point value. While this wlil be close to what you want, it won't be exact:
>>> 0.4/100.
0.0040000000000000001
If you actually want a decimal value, do this:
>>> import decimal
>>> decimal.Decimal('4') / decimal.Decimal('100')
Decimal("0.04")
That will give you an object that properly knows that 4 / 100 in base 10 is "0.04". Floating-point numbers are actually in base 2, i.e. binary, not decimal.
Solution 3
Make one or both of the terms a floating point number, like so:
4.0/100.0
Alternatively, turn on the feature that will be default in Python 3.0, 'true division', that does what you want. At the top of your module or script, do:
from __future__ import division
Solution 4
You might want to look at Python's decimal package, also. This will provide nice decimal results.
>>> decimal.Decimal('4')/100
Decimal("0.04")
Solution 5
You need to tell Python to use floating point values, not integers. You can do that simply by using a decimal point yourself in the inputs:
>>> 4/100.0
0.040000000000000001
Comments
-
Ray almost 2 years
For example, the standard division symbol '/' rounds to zero:
>>> 4 / 100 0
However, I want it to return 0.04. What do I use?
-
Torsten Marek over 15 yearsPlease note that this won't be the case anymore in Python 3.0 if you use /.
-
Ishbir over 15 yearsPlease also add the availability of
python -Q new
command-line option to make your answer more complete. -
Lomithrani over 15 yearsThis gives a floating point value, not a decimal value. See Glyph's answer.
-
Mechanical snail about 12 yearsYou can also use
from __future__ import division
in the source code. -
Jai Narayan over 6 yearsfor python3 you don't require that import division line just use print statements for python2 you need to explicitly add import statement
-
Murphy about 6 yearsPlease add details why this would solve the original issue, and perhaps what disadvantages this approach has.
-
Vadorequest over 5 yearsAnd yet people say python is a great programming language for math... I don't know what's the worse here, using a special import which may change the behaviour if removed, or specifying float operation everywhere.
-
Soham Grover almost 2 yearswhy does 0.4÷100 give 0.0040000000000000001 instead of just 0.004?
-
Glyph almost 2 yearsBecause the actual representation of the number is in binary, and since it has to use a finite amount of binary decimal places, the repeating pattern that would produce 4/1000 exactly cannot be represented, in the same way that you cannot represent 1/3 in decimal; 0.33333333333333333 is still less than 1/3; you have to keep going out to infinity. I have written some more aobut this here: blog.glyph.im/2019/10/the-numbers-they-lie.html
-
Glyph almost 2 yearsNowadays, even
repr
will round this off for you, so you can see the full decimal representation by expanding the precision with anf
string:f"{0.004:.100}"
results in'0.0040000000000000000832667268468867405317723751068115234375'
.