本文整理汇总了Python中decimal.ROUND_HALF_DOWN属性的典型用法代码示例。如果您正苦于以下问题:Python decimal.ROUND_HALF_DOWN属性的具体用法?Python decimal.ROUND_HALF_DOWN怎么用?Python decimal.ROUND_HALF_DOWN使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类decimal
的用法示例。
在下文中一共展示了decimal.ROUND_HALF_DOWN属性的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: round_value
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_DOWN [as 别名]
def round_value(value, decimalplaces=0):
"""Rounding function.
Args:
value (float): Number to round.
decimalplaces (int): Number of decimal places of float to represent rounded value.
Returns:
rounded (int/float): Rounded value.
"""
# Rounds to nearest integer (half values are rounded downwards)
if decimalplaces == 0:
rounded = int(d.Decimal(value).quantize(d.Decimal('1'), rounding=d.ROUND_HALF_DOWN))
# Rounds down to nearest float represented by number of decimal places
else:
precision = '1.{places}'.format(places='0' * decimalplaces)
rounded = float(d.Decimal(value).quantize(d.Decimal(precision), rounding=d.ROUND_FLOOR))
return rounded
示例2: create_order
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_DOWN [as 别名]
def create_order(ordered_items, total, payment_type):
"""create a modified order"""
order_dct = {
"items": ordered_items,
"payment_type":payment_type,
"total": total}
tax_scale = int((Order.taxrate * 100) + 10000)
subtotal = int(decimal.Decimal((total * 100)
/ tax_scale).quantize(
decimal.Decimal('0.01'),
rounding=decimal.ROUND_HALF_DOWN) * 100)
tax = total - subtotal
order_dct["subtotal"] = subtotal
order_dct["tax"] = tax
return order_dct
示例3: _calc_ratio
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_DOWN [as 别名]
def _calc_ratio(part, whole):
"""Calculate ratio
Returns int
"""
return int((part / whole * 100).quantize(
decimal.Decimal('1'), rounding=decimal.ROUND_HALF_DOWN))
示例4: _subtotal
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_DOWN [as 别名]
def _subtotal(self)->int:
tax_scale = int((self.taxrate * 100) + 10000)
result = Decimal((self.total * 100)
/ tax_scale).quantize(
Decimal('0.01'),
rounding=ROUND_HALF_DOWN)
return int(result * 100)
示例5: round_int
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_DOWN [as 别名]
def round_int(dec):
"""Round float to nearest int using expected rounding."""
return int(decimal.Decimal(dec).quantize(decimal.Decimal('0'), decimal.ROUND_HALF_DOWN))
示例6: test_round_modify_method
# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import ROUND_HALF_DOWN [as 别名]
def test_round_modify_method(self):
assert round_value(3.5, rounding_method=ROUND_HALF_DOWN) == 3.0