Python Decimal doesn\'t support being constructed from float; it expects that you have to convert float to a string first.
This is very inconvenient since standard
"%.15g" % f
Or in Python 3.0:
format(f, ".15g")
Just pass the float to Decimal
constructor directly, like this:
from decimal import Decimal
Decimal(f)
When you say "preserving value as the user has entered", why not just store the user-entered value as a string, and pass that to the Decimal constructor?
The "official" string representation of a float is given by the repr() built-in:
>>> repr(1.5)
'1.5'
>>> repr(12345.678901234567890123456789)
'12345.678901234567'
You can use repr() instead of a formatted string, the result won't contain any unnecessary garbage.