I am using the Python Decimal class for precise floating-point arithmetic. I need to convert the result number consistently into a standard notation number as a string. However, very small decimal numbers are rendered in scientific notation by default.
>>> from decimal import Decimal
>>>
>>> d = Decimal("0.000001")
>>> d
Decimal('0.000001')
>>> str(d)
'0.000001'
>>> d = Decimal("0.000000001")
>>> d
Decimal('1E-9')
>>> str(d)
'1E-9'
How would I get str(d)
to return '0.000000001'
?
Summary: Use the string literal syntax f"{number:. nf}" to suppress the scientific notation of a number to its floating-point representation.
Correct answer: To convert a decimal into scientific notation, move the decimal point until you get to the left of the first non-zero integer. The number of places the decimal point moves is the power of the exponent, because each movement represents a "power of 10".
'{:f}'.format(d)
Out[12]: '0.000000001'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With