Wednesday, August 10, 2022

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

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)

No comments:

Post a Comment

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