Skip to content

Numerical precision

Conversion utilities for numbers to a specified precision and notation.

from prairielearn.to_precision import ...

eng_notation

eng_notation(
    value: float, precision: int, filler: str
) -> str

Engineering notation. http://www.mathsisfun.com/definitions/engineering-notation.html

filler is placed between the decimal value and 10s exponent

Examples:

>>> eng_notation(123, 1, 'E')
100E0
>>> eng_notation(1230, 3, 'E')
1.23E3
>>> eng_notation(.126, 2, 'E')
120E-3

Returns:

Type Description
str

string of value with the proper precision and 10s exponent that is divisible by 3

created by William Rusnack github.com/BebeSparkelSparkel linkedin.com/in/williamrusnack/ williamrusnack@gmail.com

sci_notation

sci_notation(
    value: float, precision: int, filler: str
) -> str

Scientific notation. https://www.mathsisfun.com/numbers/scientific-notation.html

filler is placed between the decimal value and 10s exponent

Examples:

>>> sci_notation(123, 1, 'E')
1E2
>>> sci_notation(123, 3, 'E')
1.23E2
>>> sci_notation(.126, 2, 'E')
1.3E-1

Returns:

Type Description
str

string of value with the proper precision and 10s exponent

created by William Rusnack github.com/BebeSparkelSparkel linkedin.com/in/williamrusnack/ williamrusnack@gmail.com

std_notation

std_notation(
    value: float, precision: int, _extra: Any = None
) -> str

Standard notation (US version). http://www.mathsisfun.com/definitions/standard-notation.html

Examples:

>>> std_notation(5, 2)
5.0
>>> std_notation(5.36, 2)
5.4
>>> std_notation(5360, 2)
5400
>>> std_notation(0.05363, 3)
0.0536

Returns:

Type Description
str

string of value with the proper precision

created by William Rusnack github.com/BebeSparkelSparkel linkedin.com/in/williamrusnack/ williamrusnack@gmail.com

to_precision

to_precision(
    value: Any,
    precision: int,
    notation: Notation = "auto",
    filler: str = "e",
) -> str

Converts a value to the specified notation and precision.

Parameters:

Name Type Description Default
value Any

any type that can be converted to a float

required
precision int

integer that is greater than zero

required
notation Notation

string that specifies the notation to use

'auto'
filler str

string that is placed between the decimal value and 10s exponent

'e'

The possible notations are:

Returns:

Type Description
str

string of value with the proper precision and notation