Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get a decimal in python?

Tags:

python

In a truly surreal experience, I have spent 20 minutes on a task I thought would take 20 seconds.

I want to use decimals with 3 or more places. I cannot get anything over 1 place, and even in that case it's nonsense.

For example, I am unable to get 1/3 to show as anything other than 0 or 0.0.

Googling led me to Decimal, but Decimal also failed to help.

Please end this torture and tell me what I have to do to get 1/3 to show up as .333!

Edit Thank you all for clearing that up! Apparently computer scientists have invented something called integer division to confuse and irritate mathematicians. Thank you for being kind enough to dissipate my confusion.

like image 411
rschwieb Avatar asked Jul 15 '12 00:07

rschwieb


4 Answers

In Python 2, 1/3 does integer division because both operands are integers. You need to do float division:

1.0/3.0

Or:

from __future__ import division

Which will make / do real division and // do integer division. This is the default as of Python 3.

like image 88
Seth Carnegie Avatar answered Oct 31 '22 19:10

Seth Carnegie


If you divide 2 integers you'll end up with a truncated integer result (i.e., any fractional part of the result is discarded) which is why

 1 / 3

gives you:

 0

To avoid this problem, at least one of the operands needs to be a float. e.g., 1.0 / 3 or 1 / 3.0 or (of course) 1.0 / 3.0 will avoid integer truncation. This behavior is not unique to Python by the way.

(Edit: As mentioned in a helpful comment below by @Ivc if one of the integer operands is negative, the result is floor()'ed instead - see this article on the reason for this).

Also, there might be some confusion about the internal and external represenation of the number. The number is what it is, but we can determine how it's displayed.

You can control the external representation with formatting instructions. For instance a number can be displayed with 5 digits after the decimal point like this:

n = 1/3.0

print '%.5f' %n
0.33333

To get 15 digits after the decimal.

print '%.15f' %n
0.333333333333333

Finally, there is a "new and improved" way of formatting strings/numbers using the .format() function which will be around for probably much longer than the %-formatting I showed above. An example would be:

print 'the number is {:.2}'.format(1.0/3.0)

would give you:

the number is 0.33
like image 26
Levon Avatar answered Oct 31 '22 20:10

Levon


Your value needs to be a floating point

In [1]: 1/3
Out[1]: 0

In [2]: 1/3.0
Out[2]: 0.33333333333333331

or

In [1]: from __future__ import division

In [2]: 1/3
Out[2]: 0.33333333333333331
like image 2
Andy Casey Avatar answered Oct 31 '22 20:10

Andy Casey


In Python2 you have to specify at least one of the operands as a floating point number. To take just 3 decimals you can use the round() method:

>>> a = 1.0 / 3
>>> round(a, 3)
0.333

In Python3 it seems they don't perform integer division by default, so you can do:

>>> a = 1/3
>>> round(a, 3)
0.333
like image 2
dave Avatar answered Oct 31 '22 19:10

dave