def try_round(amount, expected): digits = max(0, -int(log10(currency.rounding))) result = float_repr(currency.round(amount), precision_digits=digits) self.assertEqual( result, expected, 'Rounding error: got %s, expected %s' % (result, expected))
def try_round(amount, expected, digits=3, method='HALF-UP'): value = float_round(amount, precision_digits=digits, rounding_method=method) result = float_repr(value, precision_digits=digits) self.assertEqual( result, expected, 'Rounding error: got %s, expected %s' % (result, expected))
def format_numbers(number): #format number to str with between 2 and 8 decimals (event if it's .00) number_splited = str(number).split('.') if len(number_splited) == 1: return "%.02f" % number cents = number_splited[1] if len(cents) > 8: return "%.08f" % number return float_repr(number, max(2, len(cents)))
def infos(self, user_id=None): self._check_user_impersonification(user_id) user = request.env['res.users'].browse( user_id) if user_id else request.env.user infos = self._make_infos(user, order=False) lines = self._get_current_lines(user.id) if lines: lines = [ { 'id': line.id, 'product': (line.product_id.id, line.product_id.name, float_repr(float_round(line.product_id.price, 2), 2)), 'toppings': [(topping.name, float_repr(float_round(topping.price, 2), 2)) for topping in line.topping_ids_1 | line.topping_ids_2 | line.topping_ids_3], 'quantity': line.quantity, 'price': line.price, 'state': line.state, # Only used for _get_state 'note': line.note } for line in lines ] raw_state, state = self._get_state(lines) infos.update({ 'total': float_repr( float_round(sum(line['price'] for line in lines), 2), 2), 'raw_state': raw_state, 'state': state, 'lines': lines, }) return infos
def _get_pricelist_item_name_price(self): for item in self: if item.categ_id and item.applied_on == '2_product_category': item.name = _("Category: %s") % (item.categ_id.display_name) elif item.product_tmpl_id and item.applied_on == '1_product': item.name = _("Product: %s") % ( item.product_tmpl_id.display_name) elif item.product_id and item.applied_on == '0_product_variant': item.name = _("Variant: %s") % (item.product_id.with_context( display_default_code=False).display_name) else: item.name = _("All Products") if item.compute_price == 'fixed': decimal_places = self.env['decimal.precision'].precision_get( 'Product Price') if item.currency_id.position == 'after': item.price = "%s %s" % ( float_repr( item.fixed_price, decimal_places, ), item.currency_id.symbol, ) else: item.price = "%s %s" % ( item.currency_id.symbol, float_repr( item.fixed_price, decimal_places, ), ) elif item.compute_price == 'percentage': item.price = _("%s %% discount") % (item.percent_price) else: item.price = _("%s %% discount and %s surcharge") % ( item.price_discount, item.price_surcharge)
def format_monetary(number, currency): # Format the monetary values to avoid trailing decimals (e.g. 90.85000000000001). return float_repr(number, currency.decimal_places)