First page Back Continue Last page Overview Graphics
Conversions
Converting to strings:
>>> import decimal
>>> str( decimal.Decimal('10.3') )
'10.3'
Exponents are used if not all digits are significant:
>>> TEN = decimal.Decimal(10)
>>> x = decimal.Decimal(259) * TEN ** -9
>>> str(x)
'2.59E-7'
Notes:
We already covered converting other types into decimals.
Converting decimals into strings is normally just a call to the standard str() function.
When not all digits are significant, the string form shows only significant digits and uses an exponent. Sometimes you might prefer the Decimal method to_eng_string(). This adjusts the decimal to ensure that the exponent is a multiple of three.