desperately simple python decimal -
hi need please. want calculate float numbers format xxxxx.yyyyyyyyyyyyyyy. have learned should use decimal instead of float tried lot, , searched lot still have following simple problem:
getcontext().prec = 14 = decimal(str('12.737791301')) b = decimal(str('12.737791839')) c = decimal((b - a)) # substract want here print a, b, c 12.737791301 12.737791839 5.38e-7 i want c displayed 0.000000538
thanks help!
ps. normalize() did not work
the real solution format(), available in python 2.6 , 3.1.
format(c,"f") convert decimal float repr-style output. part of new style formatting in python.
other worse ideas solutions below:
this can accomplished format string, if decimal values floats. print "%0.9f" % c produce '0.000000538'
the python library reference can tell more.
to around problem of needing know right number of decimal places, print out @ limit of float precision condition in case decimal smaller:
out = "%.16f" % c if c >= 1e-16 else "0.0" print out.rstrip('0') with second line dealing trailing 0s.
Comments
Post a Comment