本文整理汇总了Python中decimal.Context方法的典型用法代码示例。如果您正苦于以下问题:Python decimal.Context方法的具体用法?Python decimal.Context怎么用?Python decimal.Context使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类decimal
的用法示例。
在下文中一共展示了decimal.Context方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cast_to_decimal
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def test_cast_to_decimal(t, df, type):
expr = t.float64_as_strings.cast(type)
result = expr.execute()
context = decimal.Context(prec=type.precision)
expected = df.float64_as_strings.apply(
lambda x: context.create_decimal(x).quantize(
decimal.Decimal(
'{}.{}'.format(
'0' * (type.precision - type.scale), '0' * type.scale
)
)
)
)
tm.assert_series_equal(result, expected)
assert all(
abs(element.as_tuple().exponent) == type.scale
for element in result.values
)
assert all(
1 <= len(element.as_tuple().digits) <= type.precision
for element in result.values
)
示例2: test_astype_dispatches
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def test_astype_dispatches(frame):
# This is a dtype-specific test that ensures Series[decimal].astype
# gets all the way through to ExtensionArray.astype
# Designing a reliable smoke test that works for arbitrary data types
# is difficult.
data = pd.Series(DecimalArray([decimal.Decimal(2)]), name='a')
ctx = decimal.Context()
ctx.prec = 5
if frame:
data = data.to_frame()
result = data.astype(DecimalDtype(ctx))
if frame:
result = result['a']
assert result.dtype.context.prec == ctx.prec
示例3: float_to_str
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def float_to_str(f, p=20):
"""Convert the given float to a string, without resorting to scientific notation.
Args:
f: Float params.
p: Precision length.
Returns:
s: String format data.
"""
if type(f) == str:
f = float(f)
ctx = decimal.Context(p)
d1 = ctx.create_decimal(repr(f))
s = format(d1, 'f')
return s
示例4: test_math_functions_decimal
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def test_math_functions_decimal(t, df, ibis_func, pandas_func):
dtype = dt.Decimal(12, 3)
result = ibis_func(t.float64_as_strings.cast(dtype)).execute()
context = decimal.Context(prec=dtype.precision)
expected = df.float64_as_strings.apply(
lambda x: context.create_decimal(x).quantize(
decimal.Decimal(
'{}.{}'.format(
'0' * (dtype.precision - dtype.scale), '0' * dtype.scale
)
)
)
).apply(pandas_func)
result[result.apply(math.isnan)] = -99999
expected[expected.apply(math.isnan)] = -99999
tm.assert_series_equal(result, expected)
示例5: _get_decimal_converter
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def _get_decimal_converter(precision, scale):
if scale == 0:
return int
context = decimal.Context(prec=precision)
quantize_value = decimal.Decimal(1).scaleb(-scale)
return lambda v: decimal.Decimal(v).quantize(quantize_value, context=context)
示例6: float_to_str
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def float_to_str(f, p=20):
""" Convert the given float to a string, without resorting to scientific notation.
@param f 浮点数参数
@param p 精读
"""
if type(f) == str:
f = float(f)
ctx = decimal.Context(p)
d1 = ctx.create_decimal(repr(f))
return format(d1, 'f')
示例7: create_decimal128_context
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def create_decimal128_context():
"""Returns an instance of :class:`decimal.Context` appropriate
for working with IEEE-754 128-bit decimal floating point values.
"""
opts = _CTX_OPTIONS.copy()
opts['traps'] = []
return decimal.Context(**opts)
示例8: context
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def context(self):
return decimal.Context(prec=self.max_digits)
示例9: execute_cast_series_to_decimal
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def execute_cast_series_to_decimal(op, data, type, **kwargs):
precision = type.precision
scale = type.scale
context = decimal.Context(prec=precision)
places = context.create_decimal(
'{}.{}'.format('0' * (precision - scale), '0' * scale)
)
return data.apply(
lambda x, context=context, places=places: ( # noqa: E501
context.create_decimal(x).quantize(places)
)
)
示例10: float_to_decimal
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def float_to_decimal(f):
"Convert a floating point number to a Decimal with no loss of information"
n, d = f.as_integer_ratio()
numerator, denominator = Decimal(n), Decimal(d)
ctx = Context(prec=60)
result = ctx.divide(numerator, denominator)
while ctx.flags[Inexact]:
ctx.flags[Inexact] = False
ctx.prec *= 2
result = ctx.divide(numerator, denominator)
return result
示例11: convert_value
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def convert_value(cls, value, to, prec=33):
"""
Convert values from/to pip/bip.
Args:
value (string|int|Decimal|float): value to convert
to (string): coin to convert value to
prec (int): decimal context precision (decimal number length)
Returns:
int|Decimal
"""
# Get default decimal context
context = decimal.getcontext()
# Set temporary decimal context for calculation
decimal.setcontext(
decimal.Context(prec=prec, rounding=decimal.ROUND_DOWN)
)
# PIP in BIP in Decimal
default = decimal.Decimal(str(cls.DEFAULT))
# Value in Decimal
value = decimal.Decimal(str(value))
# Make conversion
if to == 'pip':
value = int(value * default)
elif to == 'bip':
value /= default
# Reset decimal context to default
decimal.setcontext(context)
return value
示例12: to_bip
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def to_bip(value):
"""
Convert PIPs to BIPs.
Use dynamic Decimal precision, depending on value length.
Args:
value (int|str|Decimal): value in PIP
Returns:
Decimal
"""
# Check if value is correct PIP value
value = str(value)
if not value.isdigit():
raise ValueError(f'{value} is not correct PIP value')
# Get default decimal context
context = decimal.getcontext()
# Set temporary decimal context for calculation
decimal.setcontext(
decimal.Context(prec=len(value), rounding=decimal.ROUND_DOWN)
)
# Convert value
value = decimal.Decimal(value) / decimal.Decimal(PIP)
# Reset decimal context to default
decimal.setcontext(context)
return value
示例13: __init__
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def __init__(self, precision=2):
self.empty()
self._context = Context(prec=precision)
示例14: round_py2_compat
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def round_py2_compat(what):
"""
Python 2 and Python 3 use different rounding strategies in round(). This
function ensures that results are python2/3 compatible and backward
compatible with previous py2 releases
:param what: float
:return: rounded long
"""
d = Context(
prec=len(str(long(what))), # round to integer with max precision
rounding=decimal.ROUND_HALF_UP
).create_decimal(str(what)) # str(): python 2.6 compat
return long(d)
示例15: get_decimalfield_converter
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Context [as 别名]
def get_decimalfield_converter(self, expression):
# SQLite stores only 15 significant digits. Digits coming from
# float inaccuracy must be removed.
create_decimal = decimal.Context(prec=15).create_decimal_from_float
if isinstance(expression, Col):
quantize_value = decimal.Decimal(1).scaleb(-expression.output_field.decimal_places)
def converter(value, expression, connection):
if value is not None:
return create_decimal(value).quantize(quantize_value, context=expression.output_field.context)
else:
def converter(value, expression, connection):
if value is not None:
return create_decimal(value)
return converter