Q:

Is there any Python function that will format a measurement and an uncertainty parenthetically?

For example, say I have these numbers from calculations:
17.969860 with uncertainty 5.966e-05
0.01202 with uncertainty 0.001749
and I want to format them in this way instead:
17.96986(6), 0.012(2)
Does any sort of function exist that automatically formats measurements with uncertainties in this way?
Thank you.

A:

No, you have to code it yourself:
import math
def str_with_err(value, error):
    digits = -int(math.floor(math.log10(error)))
    return "{0:.{2}f}({1:.0f})".format(value, error*10**digits, digits)

print str_with_err(17.969860, 5.966e-05)
# 17.96986(6)
print str_with_err(0.01202, 0.001749)
# 0.012(2)

