I have big integers in database (numeric(24,0)). And I want to convert this number to "human readable" format. I have function:
def from_int(value, precision):
fprec = "%." + str(precision) + "f"
return fprec % (Decimal(value) * (Decimal(10)**(-1*precision)))
And it works:
from_int(1000000, 6)
'1.000000'
from_int(1000000, 8)
'0.01000000'
from_int(1000000, 12)
'0.000001000000'
from_int(1000000, 2)
'10000.00'
but for:
from_int(19999999999999999, 2)
'200000000000000.00'
How to set precision without rounding?
Formatting with %f
converts the number to floating point losing precision. Instead use str
:
def from_int(value, precision):
return str(Decimal(value) * Decimal(10)**(-precision))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments