Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a very small python Decimal into a non-scientific notation string

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'?

like image 442
dmi_ Avatar asked Mar 18 '14 20:03

dmi_


People also ask

How do you make a number not scientific notation in Python?

Summary: Use the string literal syntax f"{number:. nf}" to suppress the scientific notation of a number to its floating-point representation.

How do you write small decimals in scientific notation?

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".


1 Answers

'{:f}'.format(d)
Out[12]: '0.000000001'
like image 131
m.wasowski Avatar answered Sep 20 '22 12:09

m.wasowski