3

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?

user2763361
  • 3,679
  • 10
  • 43
  • 77

2 Answers2

1

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

>>> fl = 0.000005
>>> print '%.6f' % fl
0.000005
flakes
  • 17,121
  • 8
  • 34
  • 75
  • What if I just want it to the exact accuracy as I specified `fl` with. So if I set `fl = 0.00214`, I want `string = '0.00214'`, or if I set `fl = 0.0000000006`, I want `string = '0.0000000006'`. – user2763361 Sep 04 '14 at 12:36
1

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 e.g. 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'>
Michel Keijzers
  • 14,510
  • 27
  • 88
  • 115