简体   繁体   中英

Casting float to string without scientific notation

The float:

fl = 0.000005

casts to String as str(fl)=='5e-06' . however, I want it to cast as str(fl)='0.000005' for exporting to CSV purposes.

How do I achieve this?

You can just use the standard string formatting option stating the precision you want

>>> fl = 0.000005
>>> print '%.6f' % fl
0.000005

Use

fl = 0.00005
s = '%8.5f' % fl
print s, type(s)

Gives

0.00005 <type 'str'>

In case you want no extra digits, use %g (although it uses exponential notation for eg 0.000005). See for example:

fl = 0.0005
s = '%g' % fl
print s, type(s)

fl = 0.005
s = '%g' % fl
print s, type(s)

Gives

0.0005 <type 'str'>
0.005 <type 'str'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM