Monday 14 January 2008

floating point format in Python

a=[0.001,0.003,0.002]
a.sort()
print a
-> [0.001, 0.002, 0.0030000000000000001]
import fpformat
b=fpformat.fix(a[2],6)
print b
-> 0.003000
b=fpformat.sci(a[2],2)
print b
-> 3.00e-003

1 comment:

Cross Z said...

If reading from files to get variables. such as:

f=open('d:\\a.txt')
a=f.readline()
aa=a.split() # assuming aa[0] is '0.123',aa[1] is '1.0', both of them are string type.
aa0=float(aa[0]) # Do use float() to convert string to a numerical type.
aa1=float(aa[1])
print aa
# you will get : 1.123

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter