DGT
DGT

Reputation: 2654

convert decimal to significant digits

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.

Upvotes: 2

Views: 680

Answers (3)

David Webb
David Webb

Reputation: 193716

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.

Upvotes: 0

Gabi Purcaru
Gabi Purcaru

Reputation: 31524

In [50]: "{0:.2e}".format(55820.02932238298323)
Out[50]: '5.58e+04'

Upvotes: 0

sth
sth

Reputation: 229643

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'

Upvotes: 3

Related Questions