Converting exponential to decimal in python

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

2.32313e + 07

2.1155e + 07

1.923e + 07

11856

112.32

How to convert exponential formats to decimal format

Extras: Is there a way by which I can convert the exponent directly to decimal when printing on UNIX with awk?

+6
python unix awk exponent
source share
3 answers

I assume that you have a list, not an array, but here it does not really matter; in version 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 version 2.6 or later, .format . I suppose you know that numbers as such, like numbers, are not in any "format" - these are the strings you get by formatting the numbers, for example. for output that are in some format. BTW, options on this %f can let you control the number of decimal places, width, alignment, etc. - It’s hard to offer exactly what you might want, without additional specifications from you.

In awk you can use printf .

+5
source share

You can use locale.format () to format numbers for output. This has the added benefit of being in accordance with any language conventions that might be expected when representing numbers. If you want to get full control in a certain place where you make a conclusion, you would be better off having the format "format"% vars ....

Example:

 >>> import locale >>> locale.setlocale(locale.LC_ALL, "") 'C/UTF-8/C/C/C/C' >>> locale.format("%f", 2.32313e+07, 1) '23231300.000000' 
+1
source share

In response to the last part of your question, awk can use the same printf format:

 awk '{printf "%f\n",$1}' exponential_file 

Where exponential_file contains:

 2.32313e+07 2.1155e+07 1.923e+07 11856 112.32 

You can convert to a variable for use later. Here is a simplified example:

 awk '{n = sprintf("%f\n",$1); print n * 2}' exponential_file 
0
source share

All Articles