本文整理汇总了Python中decimal.ROUND_HALF_EVEN属性的典型用法代码示例。如果您正苦于以下问题:Python decimal.ROUND_HALF_EVEN属性的具体用法?Python decimal.ROUND_HALF_EVEN怎么用?Python decimal.ROUND_HALF_EVEN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类decimal
的用法示例。
在下文中一共展示了decimal.ROUND_HALF_EVEN属性的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: round
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def round(number, ndigits=None):
return_int = False
if ndigits is None:
return_int = True
ndigits = 0
if hasattr(number, '__round__'):
return number.__round__(ndigits)
if ndigits < 0:
raise NotImplementedError('negative ndigits not supported yet')
exponent = Decimal('10') ** (-ndigits)
d = Decimal.from_float(number).quantize(exponent,
rounding=ROUND_HALF_EVEN)
if return_int:
return int(d)
else:
return float(d)
示例2: move
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def move(self, fromaccount, toaccount, amount, minconf=1):
"""Send coins between accounts in the same wallet.
If the receiving account does not exist, it is automatically
created (but not automatically assigned an address).
Args:
fromaccount (str): origin account
toaccount (str): destination account
amount (str or Decimal): amount to send (8 decimal points)
minconf (int): ensure the account has a valid balance using this
many confirmations (default=1)
Returns:
bool: True if the coins are moved successfully, False otherwise
"""
amount = Decimal(amount).quantize(self.quantum, rounding=ROUND_HALF_EVEN)
return self.rpc.call("move",
fromaccount, toaccount, float(str(amount)), minconf
)
示例3: sendfrom
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def sendfrom(self, user_id, dest_address, amount, minconf=1):
"""
Send coins from user's account.
Args:
user_id (str): this user's unique identifier
dest_address (str): address which is to receive coins
amount (str or Decimal): amount to send (eight decimal points)
minconf (int): ensure the account has a valid balance using this
many confirmations (default=1)
Returns:
str: transaction ID
"""
amount = Decimal(amount).quantize(self.quantum, rounding=ROUND_HALF_EVEN)
txhash = self.rpc.call("sendfrom",
user_id, dest_address, float(str(amount)), minconf
)
self.logger.debug("Send %s %s from %s to %s" % (str(amount), self.coin,
str(user_id), dest_address))
self.logger.debug("Transaction hash: %s" % txhash)
return txhash
示例4: setUp
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def setUp(self):
self.user_id = "testaccount1"
self.other_user_id = "testaccount2"
self.coin = "Bitcoin"
self.message = "hello world!" # used for signature testing
self.txfields = ("account", "category", "amount", "time")
self.quantum = Decimal("1e-"+str(COINS[self.coin.lower()]["decimals"]))
self.amount_to_send = Decimal("0.01").quantize(self.quantum,
rounding=ROUND_HALF_EVEN)
self.txfee = Decimal(COINS[self.coin.lower()]["txfee"])
self.bridge = Bridge(coin=self.coin, testnet=True,
reconnect=False, testing=True)
self.assertFalse(self.bridge.reconnect)
self.assertEqual(self.bridge.quantum, self.quantum)
self.assertTrue(self.bridge.testnet)
self.assertEqual(self.bridge.coin, u"bitcoin")
self.assertTrue(self.bridge.connected)
self.user_address = self.bridge.getaccountaddress(self.user_id)
self.other_user_address = self.bridge.getaccountaddress(self.other_user_id)
self.bridge.walletlock()
self.btc_testnet_faucet = "msj42CCGruhRsFrGATiUuh25dtxYtnpbTx"
示例5: calculate_price
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def calculate_price(base_quantity, quote_quantity, base_divisibility, quote_divisibility, order_type=None):
if not base_divisibility:
base_quantity *= config.UNIT
if not quote_divisibility:
quote_quantity *= config.UNIT
try:
if order_type == 'BUY':
decimal.setcontext(decimal.Context(prec=8, rounding=decimal.ROUND_DOWN))
elif order_type == 'SELL':
decimal.setcontext(decimal.Context(prec=8, rounding=decimal.ROUND_UP))
price = format(D(quote_quantity) / D(base_quantity), '.8f')
decimal.setcontext(decimal.Context(prec=8, rounding=decimal.ROUND_HALF_EVEN))
return price
except Exception as e:
logging.exception(e)
decimal.setcontext(decimal.Context(prec=8, rounding=decimal.ROUND_HALF_EVEN))
raise(e)
示例6: banker_round
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def banker_round(decimal_value):
"""
Force the value to be rounded with the `ROUND_HALF_EVEN` method,
also called the Banking Rounding due to the heavy use in the
banking system
"""
return decimal_value.quantize(decimal.Decimal('0.01'),
rounding=decimal.ROUND_HALF_EVEN)
示例7: intround
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def intround(value):
"""Given a float returns a rounded int. Should give the same result on
both Py2/3
"""
return int(decimal.Decimal.from_float(
value).to_integral_value(decimal.ROUND_HALF_EVEN))
示例8: f2s
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def f2s(self, f):
return str(dec.Decimal(repr(f)).quantize(self.car_prec, dec.ROUND_HALF_EVEN))
示例9: _round_quantize
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def _round_quantize(self, val):
# round() has different behavior in python 2/3
# For consistency between 2 and 3 we use quantize, however
# it is slower than the built in round function.
d = decimal.Decimal(val)
rounded = d.quantize(1, rounding=decimal.ROUND_HALF_EVEN)
return float(rounded)
示例10: pydecimal_equivalent_rounding_mode
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def pydecimal_equivalent_rounding_mode(self):
return {
RM.RM_TowardsPositiveInf: decimal.ROUND_CEILING,
RM.RM_TowardsNegativeInf: decimal.ROUND_FLOOR,
RM.RM_TowardsZero: decimal.ROUND_DOWN,
RM.RM_NearestTiesEven: decimal.ROUND_HALF_EVEN,
RM.RM_NearestTiesAwayFromZero: decimal.ROUND_UP,
}[self]
示例11: newround
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def newround(number, ndigits=None):
"""
See Python 3 documentation: uses Banker's Rounding.
Delegates to the __round__ method if for some reason this exists.
If not, rounds a number to a given precision in decimal digits (default
0 digits). This returns an int when called with one argument,
otherwise the same type as the number. ndigits may be negative.
See the test_round method in future/tests/test_builtins.py for
examples.
"""
return_int = False
if ndigits is None:
return_int = True
ndigits = 0
if hasattr(number, '__round__'):
return number.__round__(ndigits)
if ndigits < 0:
raise NotImplementedError('negative ndigits not supported yet')
exponent = Decimal('10') ** (-ndigits)
if PYPY:
# Work around issue #24: round() breaks on PyPy with NumPy's types
if 'numpy' in repr(type(number)):
number = float(number)
if not PY26:
d = Decimal.from_float(number).quantize(exponent,
rounding=ROUND_HALF_EVEN)
else:
d = from_float_26(number).quantize(exponent, rounding=ROUND_HALF_EVEN)
if return_int:
return int(d)
else:
return float(d)
### From Python 2.7's decimal.py. Only needed to support Py2.6:
示例12: newround
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def newround(number, ndigits=None):
"""
See Python 3 documentation: uses Banker's Rounding.
Delegates to the __round__ method if for some reason this exists.
If not, rounds a number to a given precision in decimal digits (default
0 digits). This returns an int when called with one argument,
otherwise the same type as the number. ndigits may be negative.
See the test_round method in future/tests/test_builtins.py for
examples.
"""
return_int = False
if ndigits is None:
return_int = True
ndigits = 0
if hasattr(number, '__round__'):
return number.__round__(ndigits)
if ndigits < 0:
raise NotImplementedError('negative ndigits not supported yet')
exponent = Decimal('10') ** (-ndigits)
if PYPY:
# Work around issue #24: round() breaks on PyPy with NumPy's types
if 'numpy' in repr(type(number)):
number = float(number)
if not PY26:
d = Decimal.from_float(number).quantize(exponent,
rounding=ROUND_HALF_EVEN)
else:
d = from_float_26(number).quantize(exponent, rounding=ROUND_HALF_EVEN)
if return_int:
return int(d)
else:
return float(d)
### From Python 2.7's decimal.py. Only needed to support Py2.6:
示例13: newround
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_EVEN [as 别名]
def newround(number, ndigits=None):
"""
See Python 3 documentation: uses Banker's Rounding.
Delegates to the __round__ method if for some reason this exists.
If not, rounds a number to a given precision in decimal digits (default
0 digits). This returns an int when called with one argument,
otherwise the same type as the number. ndigits may be negative.
See the test_round method in future/tests/test_builtins.py for
examples.
"""
return_int = False
if ndigits is None:
return_int = True
ndigits = 0
if hasattr(number, '__round__'):
return number.__round__(ndigits)
if ndigits < 0:
raise NotImplementedError('negative ndigits not supported yet')
exponent = Decimal('10') ** (-ndigits)
if PYPY:
# Work around issue #24: round() breaks on PyPy with NumPy's types
if 'numpy' in repr(type(number)):
number = float(number)
if isinstance(number, Decimal):
d = number
else:
if not PY26:
d = Decimal.from_float(number).quantize(exponent,
rounding=ROUND_HALF_EVEN)
else:
d = from_float_26(number).quantize(exponent, rounding=ROUND_HALF_EVEN)
if return_int:
return int(d)
else:
return float(d)
### From Python 2.7's decimal.py. Only needed to support Py2.6: