formatnum - Format values and uncertainties nicely for printing

format_value

Given value v and uncertainty dv, return a string v which is the value formatted with the appropriate number of digits.

format_uncertainty

Value and uncertainty formatter.

format_uncertainty_compact

Given value v and uncertainty dv, return the compact representation v(##), where ## are the first two digits of the uncertainty.

format_uncertainty_pm

Given value v and uncertainty dv, return a string v +/- dv.

Format values and uncertainties nicely for printing.

The formatted value uses only the number of digits warranted by the uncertainty in the measurement.

format_value() shows the value without the uncertainty.

format_uncertainty_pm() shows the expanded format v +/- err.

format_uncertainty_compact() shows the compact format v(##), where the number in parenthesis is the uncertainty in the last two digits of v.

format_uncertainty() uses the compact format by default, but this can be changed to use the expanded +/- format by setting format_uncertainty.compact to False. This is a global setting which should be considered a user preference. Any library code that depends on a specific format style should use the corresponding formatting function.

If the uncertainty is 0 or not otherwise provided, the simple %g floating point format option is used.

Infinite and indefinite numbers are represented as inf and NaN.

Example:

>>> v,dv = 757.2356,0.01032
>>> print(format_uncertainty_pm(v,dv))
757.236 +/- 0.010
>>> print(format_uncertainty_compact(v,dv))
757.236(10)
>>> print(format_uncertainty(v,dv))
757.236(10)
>>> format_uncertainty.compact = False
>>> print(format_uncertainty(v,dv))
757.236 +/- 0.010
>>> format_uncertainty.compact = True  # restore default
bumps.dream.formatnum.format_uncertainty(value, uncertainty)[source]

Value and uncertainty formatter.

Either the expanded v +/- dv form or the compact v(##) form will be used depending on whether format_uncertainty.compact is True or False. The default is True.

bumps.dream.formatnum.format_uncertainty_compact(value, uncertainty)[source]

Given value v and uncertainty dv, return the compact representation v(##), where ## are the first two digits of the uncertainty.

bumps.dream.formatnum.format_uncertainty_pm(value, uncertainty)[source]

Given value v and uncertainty dv, return a string v +/- dv.

bumps.dream.formatnum.format_value(value, uncertainty)[source]

Given value v and uncertainty dv, return a string v which is the value formatted with the appropriate number of digits.