5

Similar posts such as the following do not answer my question. Convert a string to integer with decimal in Python

Consider the following Python code.

>>> import decimal
>>> s = '23.456'
>>> d = decimal.Decimal(s)
>>> d
Decimal('23.456')           # How do I represent this as simply 23.456?
>>> d - 1
22                          # How do I obtain the output to be 22.456?

How do I convert a string to a decimal number, so I am able to perform arithmetic functions on it and obtain an output with the correct precision?

Community
  • 1
  • 1
idealistikz
  • 1,247
  • 5
  • 21
  • 35

7 Answers7

4

If you want to stay in decimal numbers, safest is to convert everything:

>>> s = '23.456'
>>> d = decimal.Decimal(s)

>>> d - decimal.Decimal('1')
Decimal('22.456')
>>> d - decimal.Decimal('1.0')
Decimal('22.456')

In Python 2.7, there's an implicit conversion for integers, but not floats.

>>> d - 1
Decimal('22.456')
>>> d - 1.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'Decimal' and 'float'
Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59
3

Is the Decimal required for your computations? The Decimal fixed point and floating point arithmetic doc outlines their differences. If not, you could just do

 d = float('23.456')
 d
 23.456

 d - 1
 22.456

Oddly enough re Decimal, I get this interactively

d = decimal.Decimal('23.456')

d
Decimal('23.456')
d - 1
Decimal('22.456')

But when I print it, I get the values

print d
23.456
print d-1
22.456
Levon
  • 138,105
  • 33
  • 200
  • 191
  • 1
    float is NOT the same thing: float('100000000.0') + float('0.000000001') is 100000000.0 – wroniasty Jun 10 '12 at 19:06
  • @wroniasty I did NOT claim it was, I simply asked if it was required. The differences between the two are outlined quite clearly here: http://docs.python.org/library/decimal.html – Levon Jun 10 '12 at 19:07
  • @wroniasty: If OP is working with decimals to the thousandths place, I don't really see it making that much of a difference. – Joel Cornett Jun 10 '12 at 19:08
  • 1
    Of course it makes a difference: float(1e15) + float('0.001') is 1e15 – wroniasty Jun 10 '12 at 19:08
  • 2
    There are a lot of reasons to use decimal instead of float. For example, there is really no good reason to use floating point numbers for financial arithmetic, and in some places the legal requirements make a floating point implementation quite difficult compared to a decimal implementation. – Dietrich Epp Jun 10 '12 at 19:20
  • 1
    The code `decimal.Decimal(float('23.456'))` is fairly lossy. The `float` call doesn't add anything, and it actually removes the precision information from the string. You should never use `Decimal(float(x))`. – Dietrich Epp Jun 10 '12 at 19:23
  • 2
    `>>> print d` vs `>>> d` is the difference between `str` and `repr` – Andrew Jaffe Jun 10 '12 at 19:29
1

Are you specifically TRYING specifically to use the Decimal arbitrary precision library or are you just struggling to convert a string to a Python float?

If you are TRYING to use Decimal:

>>> import decimal
>>> s1='23.456'
>>> s2='1.0'
>>> decimal.Decimal(s1) - decimal.Decimal(s2)
Decimal('22.456')
>>> s1='23.456'
>>> s2='1'
>>> decimal.Decimal(s1) - decimal.Decimal(s2)
Decimal('22.456')

Or, what I think is more likely, you are trying to just convert a string to a Python floating point value:

>>> s1='23.456'
>>> s2='1'
>>> float(s1)-float(s2)
22.456
>>> float(s1)-1
22.456
>>> float(s1)-1.0
22.456
the wolf
  • 34,510
  • 13
  • 53
  • 71
0

Use the bultin float function:

>>> d = float('23.456')
>>> d
23.456
>>> d - 1
22.456

See the docs here: http://docs.python.org/library/functions.html#float

Trevor
  • 9,518
  • 2
  • 25
  • 26
0

My Python seems to do it differently:

>>> s = '23.456'
>>> d = decimal.Decimal(s)
>>> d
Decimal('23.456')
>>> d-1
Decimal('22.456')

What version/OS are you using?

wroniasty
  • 7,884
  • 2
  • 32
  • 24
0

If using float, when the number gets too large -- x = 29345678.91 for example -- you get results that you might not expect. In this case, float(x) becomes 2.934567891E7 which seems undesirable especially if working with financial numbers.

Alfergon
  • 5,463
  • 6
  • 36
  • 56
0

In python, to convert a value string to float just do it:

num = "29.0"
print (float(num))

To convert string to decimal

from decimal import Decimal
num = "29.0"
print (Decimal(num))
Ruhul Amin
  • 821
  • 11
  • 14