跳转至

数值精度

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

示例:

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

返回:

类型 描述
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

示例:

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

返回:

类型 描述
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

示例:

>>> 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

返回:

类型 描述
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.

参数:

名称 类型 描述 默认
value Any

any type that can be converted to a float

必需
precision int

integer that is greater than zero

必需
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:

返回:

类型 描述
str

string of value with the proper precision and notation