PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 7, 2022

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

 August 07, 2022     decimal, digits, formatting, python, scientific-notation     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing