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?

  • 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 Oct 11 '16 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 Oct 11 '16 at 18:31
  • Yes. Want to convert exponential number to decimal. – John Oct 11 '16 at 18:36
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)

  • All these command need to be run at once ? I m trying to run them in VIM. – John Oct 11 '16 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. – Scott Oct 11 '16 at 23:32
1

try this

echo "3.85196e+06" | perl -ne 'printf "%d\n", $_;'
  • While this demonstrate a specific conversion, you might answer the more general question of "converting (any) exponential numbers to decimal". – Jeff Schaller Mar 1 '18 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.