def make_time_string(days=0, hours=0, minutes=0, seconds=0):
total_seconds = (
(days * 24 * 60 * 60) +
(hours * 60 * 60) +
(minutes * 60) +
seconds
)
hours = total_seconds // (60 * 60)
total_seconds -= hours * 60.0 * 60.0
minutes = total_seconds // 60
total_seconds -= minutes * 60.0
seconds = total_seconds
hours = decimal.Decimal(hours).quantize(decimal.Decimal('1.'), decimal.ROUND_UP)
minutes = decimal.Decimal(minutes).quantize(decimal.Decimal('1.'), decimal.ROUND_UP)
seconds = decimal.Decimal(seconds).quantize(decimal.Decimal('1.'), decimal.ROUND_UP)
return '{:02f}:{:02f}:{:02f}'.format(hours, minutes, seconds)
python类ROUND_UP的实例源码
def get(self):
setting = db_session.query(DBSetting).get('credit-payoff')
if setting is None:
pymt_settings_json = json.dumps({'increases': [], 'onetimes': []})
else:
pymt_settings_json = setting.value
pymt_settings_kwargs = self._payment_settings_dict(pymt_settings_json)
ih = InterestHelper(db_session, **pymt_settings_kwargs)
mps = sum(ih.min_payments.values())
payoffs = self._payoffs_list(ih)
return render_template(
'credit-payoffs.html',
monthly_pymt_sum=mps.quantize(Decimal('.01'), rounding=ROUND_UP),
payoffs=payoffs,
pymt_settings_json=pymt_settings_json
)
def __init__(self, min_value=None, max_value=None, force_string=False,
precision=2, rounding=decimal.ROUND_HALF_UP, **kwargs):
"""
:param min_value: Validation rule for the minimum acceptable value.
:param max_value: Validation rule for the maximum acceptable value.
:param force_string: Store as a string.
:param precision: Number of decimal places to store.
:param rounding: The rounding rule from the python decimal library:
- decimal.ROUND_CEILING (towards Infinity)
- decimal.ROUND_DOWN (towards zero)
- decimal.ROUND_FLOOR (towards -Infinity)
- decimal.ROUND_HALF_DOWN (to nearest with ties going towards zero)
- decimal.ROUND_HALF_EVEN (to nearest with ties going to nearest even integer)
- decimal.ROUND_HALF_UP (to nearest with ties going away from zero)
- decimal.ROUND_UP (away from zero)
- decimal.ROUND_05UP (away from zero if last digit after rounding towards zero would have been 0 or 5; otherwise towards zero)
Defaults to: ``decimal.ROUND_HALF_UP``
"""
self.min_value = min_value
self.max_value = max_value
self.force_string = force_string
self.precision = precision
self.rounding = rounding
super(DecimalField, self).__init__(**kwargs)
def float2dec(ft, decimal_digits):
"""
Convert float (or int) to Decimal (rounding up) with the
requested number of decimal digits.
Arguments:
ft (float, int): Number to convert
decimal (int): Number of digits after decimal point
Return:
Decimal: Number converted to decima
"""
with decimal.localcontext() as ctx:
ctx.rounding = decimal.ROUND_UP
places = decimal.Decimal(10)**(-decimal_digits)
return decimal.Decimal.from_float(float(ft)).quantize(places)
# Sorting algos for rectangle lists
def currency_round_up(money):
return money.quantize(Decimal('1.00'), rounding=ROUND_UP)
def consume_items(self, offer, basket, affected_lines):
"""
Same as ValueCondition.consume_items, except that it returns a list of consumed items. This
is needed for CompoundCondition to be able to correctly consume items.
"""
applicable_lines = self.get_applicable_lines(offer, basket, most_expensive_first=True)
applicable_line_ids = set(line.id for __, line in applicable_lines)
value_consumed = D('0.00')
affected_lines = list(affected_lines)
for line, __, qty in affected_lines:
if line.id in applicable_line_ids:
price = self._get_unit_price(offer, line)
value_consumed += price * qty
to_consume = max(0, self.value - value_consumed)
if to_consume == 0:
return affected_lines
for price, line in applicable_lines:
quantity_to_consume = (to_consume / price).quantize(D(1), ROUND_UP)
quantity_to_consume = min(line.quantity_without_discount, quantity_to_consume)
line.consume(quantity_to_consume)
affected_lines.append((line, 0, quantity_to_consume))
to_consume -= price * quantity_to_consume
if to_consume <= 0:
break
return affected_lines
def func_roundup(a, places=0):
""" functor for round up with decimal places """
return Decimal(a).quantize(Decimal(10) ** -places, rounding=ROUND_UP)
def round_decimal(decimal_price):
""" Round the price to two decimals. """
if not isinstance(decimal_price, Decimal):
decimal_price = Decimal(str(decimal_price))
return decimal_price.quantize(Decimal('.01'), rounding=ROUND_UP)
def _round_precision(self, float_number, fill_count):
""" Rounds the number for precision. """
if not isinstance(float_number, Decimal):
float_number = Decimal(str(float_number))
rounded = float_number.quantize(Decimal('.001'), rounding=ROUND_UP)
return str(rounded).zfill(fill_count)
def normalized(d, where=None, place=8):
amo_pla = (Decimal("10") ** -place)
if where == 'C': where = ROUND_CEILING
if where == 'DOWN': where = ROUND_DOWN
if where == 'F': where = ROUND_FLOOR
if where == 'HD': where = ROUND_HALF_DOWN
if where == 'HE': where = ROUND_HALF_EVEN
if where == 'HP': where = ROUND_HALF_UP
if where == 'UP': where = ROUND_UP
if where == '0UP': where = ROUND_05UP
return (Decimal(d).quantize(amo_pla, rounding=where or ROUND_UP)).normalize()
def clamp_amount(amount):
"""
Round the amount to integer pence,
rounding fractional pence up (away from zero) for any fractional pence value
that is greater than or equal to a tenth of a penny.
@param amount: Decimal amount to round
"""
tenths_of_pennies = (amount * Decimal('1000')).to_integral_value(rounding=ROUND_DOWN)
pounds = tenths_of_pennies / Decimal('1000')
return pounds.quantize(Decimal('1.00'), rounding=ROUND_UP)