本文整理匯總了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