I have a very silly question, suppose if i have a number 1.70000043572e-05
how should I convert it into float i.e. 0.0000170000043572
.
Asked
Active
Viewed 3.3k times
4

Bhargav Rao
- 50,140
- 28
- 121
- 140

U-571
- 519
- 2
- 7
- 23
-
3`1.70000043572e-05` is a legal float just like `0.0000170000043572`. Are these "numbers" strings or floats? – eumiro Feb 04 '15 at 12:31
-
1these numbers are strings – U-571 Feb 04 '15 at 12:32
2 Answers
10
You need to convert to a float and use str.format specifying the precision:
In [41]: print "{:f}".format(float("1.70000043572e-05"))
0.000017
# 16 digits
In [38]: print "{:.16f}".format(float("1.70000043572e-05"))
0.0000170000043572
Just calling float would give 1.70000043572e-05
.
Using older style formatting:
In [45]: print( "%.16f" % float("1.70000043572e-05"))
0.0000170000043572

Padraic Cunningham
- 176,452
- 29
- 245
- 321
5
If you are just inputting the number into your script python will understand that as a float.
If it is a string then use the builtin float
on it to do the conversion for example:
x = float("0.423423e4")
print "%.2f" % (x)
will output
4234.23

Simon Gibbons
- 6,969
- 1
- 21
- 34
-
4
-
1just calling float would not actually work , that will still give the exponential format – Padraic Cunningham Feb 04 '15 at 12:44