Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert decimal string in python to a number? [duplicate]

Tags:

python

Possible Duplicate:
Python - Parse String to Float or Int

How can I convert '1.03' (string) to a number in Python, preferably a decimal ?

like image 376
Bunny Rabbit Avatar asked Jul 22 '11 18:07

Bunny Rabbit


1 Answers

Just use float()

>>> float('1.03')
1.03

If you need an actual Decimal type, use the decimal module:

>>> from decimal import *
>>> Decimal('1.03')
Decimal('1.03')

In general, using floats will be easier for you, but you pay for that with the inexact precision that comes with floats.

like image 144
Daniel DiPaolo Avatar answered Oct 10 '22 11:10

Daniel DiPaolo