strings (dollar.cents) into floats
Ben Finney
bignose+hates-spam at benfinney.id.au
Thu Aug 30 19:41:01 EDT 2007
luca bertini <luca.bertini at email.it> writes:
> i have strings which look like money values (ie 34.45)
> is there a way to convert them into float variables?
You most likely do *not* want floating-point numbers for currency,
since they rely on the operating system's binary floating point
support which cannot accurately represent decimal fractions.
>>> float("34.45")
34.450000000000003
Use the arbitrary-precision decimal.Decimal type instead.
>>> from decimal import Decimal
>>> Decimal("34.45")
Decimal("34.45")
They support the full range of arithmetic operations, without
cumulative precision errors that floats are prone to.
--
\ "As the evening sky faded from a salmon color to a sort of |
`\ flint gray, I thought back to the salmon I caught that morning, |
_o__) and how gray he was, and how I named him Flint." -- Jack Handey |
Ben Finney
More information about the Python-list
mailing list