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,604
  • 13
  • 41
  • 60
  • 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) – David 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
  • 222,467
  • 53
  • 283
  • 367
0
In [50]: "{0:.2e}".format(55820.02932238298323)
Out[50]: '5.58e+04'
Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
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
David Webb
  • 190,537
  • 57
  • 313
  • 299