Help with change significant figure to normal figure command

Hi,

Below is my input file:
Long list of significant figure

1.757E-4
7.51E-3
5.634E-5
.
.
.

Desired output file:

0.0001757
0.00751
0.00005634
.
.
.

Do anybody know how to change significant figure to normal figure through any command line language?

Thanks for any advice.

A Python:

# cat num.py 
#!/usr/bin/env python
from decimal import Decimal

def main():
   numbers = '''1.757E-4
                7.51E-3
                5.634E-5'''

   numbers= [num.strip() for num  in numbers.split('\n')]
   for num in numbers:
       print "scientific: %s decimal: %s" % (num,str(Decimal(num)))

if __name__=="__main__":
    main()
#./num.py                                                                                          
scientific: 1.757E-4 decimal: 0.0001757
scientific: 7.51E-3 decimal: 0.00751
scientific: 5.634E-5 decimal: 0.00005634

Hi Klashxx,

Thanks for your python script :slight_smile:

Can I know that how can I let your python script to take my input file data as the input of your python script?

My input file is long list of significant figure which need to change to normal figure.
Sorry that I don't state clear in my post.

Thanks and looking forward to hear from you.

Something like this:

# cat num.dat 
1.757E-4
7.51E-3
5.634E-5
# ./num.py num.dat                                                                                    
0.0001757
0.00751
0.00005634
# cat num.py                                                                                         
#!/usr/bin/env python

import sys
from decimal import Decimal

def main():
   try:
       f = open(sys.argv[1],'rb')
   except Exception,err:
       print err
       sys.exit(5)

   for number in f:
       print str(Decimal(number.strip()))
   

   f.close()
if __name__=="__main__":
    main()
awk '{$1=$1+0}1' OFMT="%.8f" file
0.00017570
0.00751000
0.00005634

Hi RudiC,

I just try with your awk command.
It return something as below:

awk '{$1=$1+0}1' OFMT="%.8f" file
0.0001757
0.00751
5.634e-05

The output result is different from what I expected.
Apart from that, is it possible your command work for something like "1.757E-100" and return "7.51E-3" as "0.00751" instead of "0.00751000".

Really thanks and sorry for troubling you.

---------- Post updated at 09:04 AM ---------- Previous update was at 08:57 AM ----------

Hi Klashxx,

I just try your python script with the data set.
But it return the following error message:

  File "./num.py", line 9
    except Exception,err:
    ^
IndentationError: unexpected indent

Is there something wrong with my python program?
Thanks for advice.

What you show there is the "%.8g" behaviour. It will not fill the field length with 0 as "%f" does, but it will resort to e- notation if the exponent ist less than -4. I'm afraid that can't be modified; you may need to do string manipulations, then. Or use sth. like python etc..

1 Like

Hi RudiC,

Really many thanks and appreciate your advice.
I guess you're right.

It will resort to e- notation if the exponent ist less than -4.
In that case, I will try to see whether python or any other way to solve my doubts.
Really appreciate your assist.
Thanks again.

How about something like this:

$ printf "%.8f\n" $(< num.dat) | sed -e 's/0*$//' -e 's/\.$/.0/'
0.0001757
0.00751
0.00005634
1 Like

Thanks, Chubler_XL :slight_smile:
Your awk command work well :slight_smile:

Try with this less verbose example:

#!/usr/bin/env python
import sys
from decimal import Decimal
with open(sys.argv[1],'rb') as f:
   for number in f:print str(Decimal(number))

About Python indentation Python syntax and semantics - Wikipedia, the free encyclopedia

1 Like