Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert exponential to decimal in python

I have an array in python that contains a set of values, some of them are

2.32313e+07

2.1155e+07

1.923e+07

11856

112.32

How do I convert the exponential formats to the decimal format

Additional: Is there a way I can convert the exponent directly to decimal when printing out in UNIX with awk?

like image 564
randomThought Avatar asked Oct 15 '09 15:10

randomThought


1 Answers

I imagine you have a list rather than an array, but here it doesn't make much of a difference; in 2.6 and earlier versions of Python, something like:

>>> L = [2.32313e+07, 2.1155e+07, 1.923e+07, 11856, 112.32]
>>> for x in L: print '%f' % x
... 
23231300.000000
21155000.000000
19230000.000000
11856.000000
112.320000

and in 2.6 or later, the .format method. I imagine you are aware that the numbers per se, as numbers, aren't in any "format" -- it's the strings you obtain by formatting the numbers, e.g. for output, that are in some format. BTW, variants on that %f can let you control number of decimals, width, alignment, etc -- hard to suggest exactly what you may want without further specs from you.

In awk, you can use printf.

like image 148
Alex Martelli Avatar answered Sep 28 '22 08:09

Alex Martelli