1

How change a number to scientific number in linux?

Number

0.000111309

scientific number

1.11E-04

Used awk,python or perl. Thanks.

And, how to get the minimum value from many Scientific Number in python? Just like sort -g | sed -n '$p' in bash.

whether python can change automatically number to Scientific Number when print? I'm find the 0 will become to 0.000000e+00 when print.

hope
  • 67
  • 1
  • 7
  • 1
    This can be be done by string formatting as explained in http://stackoverflow.com/questions/6913532/display-a-decimal-in-scientific-notation – Aimee Borda Mar 15 '17 at 15:57

3 Answers3

1

If you need scientific notation you need to use the %e or %E format specifier

Using

awk 'BEGIN{printf("%.2E\n",0.000111309)}'
1.11E-04

Using

akshay@db-3325:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import decimal
>>> '%.2E' % decimal.Decimal('0.000111309')
'1.11E-04'
>>> 

Using

akshay@db-3325:~$ perl -e 'printf("%.2E\n",0.000111309)'
1.11E-04

Using

akshay@db-3325:~$ printf "%.2E\n" 0.000111309
1.11E-04
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
0
print "%.4g" % 80085

Will output:

8.008e+04
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Asive
  • 1
0

bash can do it as well

$ printf "%E\n" 0.000111309
1.113090E-04

with rounding to two decimal digits

$ printf "%.2E\n" 0.000111309
1.11E-04
karakfa
  • 66,216
  • 7
  • 41
  • 56