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