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

Wednesday, August 10, 2022

[FIXED] How to suppress the exponential format when using decimal module in Python

 August 10, 2022     decimal, floating-point, python     No comments   

Issue

I am writing python code that requires precision up to 108 digits beyond the decimal point. For this, I am using the "decimal" module. In some calculations (example below), I get the result in the exponential format. Example:

from decimal import *
getcontext().prec=100
result1 = Decimal(387) / Decimal(1577917828000)
#results are returned in JSON

The resulting value is 2.452599198340510796231399192987633827532874544592571774909941634806093337326815474702907026157258171E-10 However, I need this in the normal (float?) format without the E-10. Any suggestions?


Solution

You can try this,

from decimal import *
getcontext().prec=100
result1 = Decimal(387) / Decimal(1577917828000)
print('{:f}'.format(result1))

You need to use this,

'{:f}'.format(result1)


Answered By - Nott
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