2

I have space-separated file. This file contains text as well as numbers in exponential format. I want to convert exponential numbers to actual decimal values. Can someone suggest how I can achieve this?

3
  • 2
    if the file contains text that should not be converted, then we'd need to see sample input so that it's clearer how to pick out the values that do need to be converted.
    – Jeff Schaller
    Commented Oct 11, 2016 at 18:26
  • did you mean you have numbers in scientific notation and want them truned into decimal.. for example 1.2e-3 into 0.0012 ??
    – Rob
    Commented Oct 11, 2016 at 18:31
  • Yes. Want to convert exponential number to decimal.
    – John
    Commented Oct 11, 2016 at 18:36

2 Answers 2

7

With GNU awk:

gawk -v RS='[-+]?[0-9.]+[eE][-+]?[0-9]+' \
     -v ORS= \
     -v CONVFMT=%.1000g '{print $0 (RT == "" ? "" : +RT)}'

gawk stores what is matched by the record separator into the RT variable which we convert to floating point without exponent with the CONVFMT.

For higher precision, for instance for 1e123 to be turned into 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 instead of 999999999999999977709969731404129670057984297594921577392083322662491290889839886077866558841507631684757522070951350501376, with gawk 4.1 or above add -M -v PREC=1000 (1000 being the number of bits)

2
  • All these command need to be run at once ? I m trying to run them in VIM.
    – John
    Commented Oct 11, 2016 at 20:08
  • It's all one command, and yes, you have to run the whole thing. You should probably put it into a script. Commented Oct 11, 2016 at 23:32
2

try this

echo "3.85196e+06" | perl -ne 'printf "%d\n", $_;'
2
  • While this demonstrate a specific conversion, you might answer the more general question of "converting (any) exponential numbers to decimal".
    – Jeff Schaller
    Commented Mar 1, 2018 at 17:09
  • this command works fine with positive exponential numbers, but its not working with negative exponentials. Can you explain how your command works. I dont know perl. Thanks
    – Richardd
    Commented May 17, 2021 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .