Sunday, August 7, 2022

[FIXED] How to print decimal notation rather than scientific notation in Python?

Issue

Assume to have this number:

p=0.00001

Now, if we print this variable we get 1e-05 (i.e., that number in scientific notation). How can I print exactly 0.00001 (i.e., in decimal notation)? Ok, I know I can use format(p, '.5f'), but the fact is that I don't know in advance the number of decimal digits (e.g., I may have 0.01 or 0.0000001, etc.). Therefore, I could count the number of decimal digits and use that in the format function. In doing so, I have a related problem... is there any way to count the decimal digits of a decimal number without using Decimal?


Solution

p = 0.0000000000000000000001

s = str(p)

print(format(p, "." + s.split("e")[-1][1:]+"f")) if "e" in s else print(p)


Answered By - Padraic Cunningham
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.