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

Tuesday, August 9, 2022

[FIXED] How to prevent/suppress Decimal from using scientific notation?

 August 09, 2022     decimal, python, scientific-notation     No comments   

Issue

In [1]: from decimal import Decimal

In [2]: Decimal('0.00000100') * Decimal('0.95')
Out[2]: Decimal('9.500E-7')

In [3]: Decimal('9.500E-7').quantize(Decimal('0.00000000'))
Out[3]: Decimal('9.5E-7')

How to suppress scientific notation, and get '0.00000095'?


Solution

You can format your output with the Format Specification Mini-Language

from decimal import Decimal
d = Decimal('9.5E-7')
print(d)
print(format(d, 'f'))

Output:

9.5E-7
0.00000095

But if you're planing to override decimal.Decimal.__str__ or decimal.Decimal.__repr__ you'll have a hard time, because they are read-only.



Answered By - Mike Scotty
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • 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