How to convert scientific notation to standard?
-
Hello,
I have a text file with data like this:
*Name: 1
55.0021 1.1E1
103.9542 4.0E-1
…Name: 1
…*I want to convert the numbers in scientific notation into standard decimals. There is no other way for me to export the data. So I need to do it here. Could you help me?
Thanks!
-
depending on your real data and that you have python script plugin installed
this might do what you wanteditor.rereplace('\d+?\.\d+?E\-*\d', lambda m: float(m.group()))
-
@Ekopalypse said in How to convert scientific notation to standard?:
editor.rereplace(‘\d+?.\d+?E-*\d’, lambda m: float(m.group()))
I just installed the plugin and tried the code. It works perectly. Thanks a lot!
-
@Ekopalypse
great idea. The scientific notation in my doc is slightly different, It has:1.0000E-001
and
1.0000E+001as examples.
What should the script be please? -
Instead of
-*
, which looks for 0 or more minus signs (I personally would have used-?
, so it’s 0 or 1), use[+-]?
, which would be 0 or 1 minus or plus signs. And then instead of the final\d
, use\d+
to allow more than one digit in the exponent. -
@PeterJones thanks. If I’m understanding correctly, this should be the final script:
editor.rereplace('\d+?\.\d+?E\[+-]?\d+', lambda m: float(m.group()))
but it doesn’t seem to do anything to my doc. Have I made a mistake?
-
@Ben-Mcmillan
m.group()
needs to take an integer as its first and only argument.
For example,m.group(0)
returns the full text of the match, andm.group(n)
returns then^th
capture group. -
This regex will capture all scientific notation numbers, and can also handle leading + signs and number with leading decimal points:
([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)
Seriously, everyone should just write that down somewhere so they can look it up in times of need.