2

Is there a function in python that could convert decimal to 3-4 significant digits, eg:

55820.02932238298323 to 5.58e4

Many thanks in advance.

DGT
  • 2,484
  • 12
  • 40
  • 57
  • 1
    possible duplicate of [How to round a number to significant figures in Python](http://stackoverflow.com/questions/3410976/how-to-round-a-number-to-significant-figures-in-python) – Dave Webb Apr 14 '11 at 15:34

3 Answers3

3

If by "convert to" you mean that you want a string formatted like that, you can use the %e format option:

>>> '%.2e' % 55820.02932238298323
'5.58e+04'
sth
  • 211,504
  • 50
  • 270
  • 362
0
In [50]: "{0:.2e}".format(55820.02932238298323)
Out[50]: '5.58e+04'
Gabi Purcaru
  • 29,852
  • 9
  • 74
  • 91
0

If this is for output purposes you can just use string formatting:

>>> "%.2e" % 55820.02932238298323
'5.58e+04'
>>> "{:.2e}".format(55820.02932238298323)
'5.58e+04'

If you want the rounded value to be a float take a look at this question.

Community
  • 1
  • 1
Dave Webb
  • 185,507
  • 57
  • 307
  • 296