rschwieb
rschwieb

Reputation: 786

How do you get a decimal in 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.

Upvotes: 13

Views: 53385

Answers (5)

recobayu
recobayu

Reputation: 611

Use Decimal. You can got 100 digit behind the dot.

from decimal import *
getcontext().prec = 100
Decimal(1)/Decimal(3)

Upvotes: 1

Levon
Levon

Reputation: 143152

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

Upvotes: 7

Seth Carnegie
Seth Carnegie

Reputation: 75150

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.

Upvotes: 22

dave
dave

Reputation: 2269

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

Upvotes: 2

Andy Casey
Andy Casey

Reputation: 307

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

Upvotes: 2

Related Questions