use rounding protection only for currencies with <2 decimal places

This commit is contained in:
Mira Weller
2023-11-13 16:37:48 +01:00
parent 6d883fc37d
commit 981a445b49

View File

@@ -48,11 +48,18 @@ def money_filter(value: Decimal, arg='', hide_currency=False):
places = settings.CURRENCY_PLACES.get(arg, 2) places = settings.CURRENCY_PLACES.get(arg, 2)
rounded = value.quantize(Decimal('1') / 10 ** places, ROUND_HALF_UP) rounded = value.quantize(Decimal('1') / 10 ** places, ROUND_HALF_UP)
if places < 2 and rounded != value: if places < 2 and rounded != value:
places = 2 # We display decimal places even if we shouldn't for this currency if rounding
# would make the numbers incorrect. If this branch executes, it's likely a bug in
# pretix, but we won't show wrong numbers!
if hide_currency:
return floatformat(value, 2)
else:
return '{} {}'.format(arg, floatformat(value, 2))
if hide_currency: if hide_currency:
return floatformat(value, places) return floatformat(value, places)
locale_parts = translation.get_language().split('-', 1) locale_parts = translation.get_language().split("-", 1)
locale = locale_parts[0] locale = locale_parts[0]
if len(locale_parts) > 1 and len(locale_parts[1]) == 2: if len(locale_parts) > 1 and len(locale_parts[1]) == 2:
try: try:
@@ -61,20 +68,9 @@ def money_filter(value: Decimal, arg='', hide_currency=False):
pass pass
try: try:
if rounded != value:
# We display decimal places even if we shouldn't for this currency if rounding
# would make the numbers incorrect. If this branch executes, it's likely a bug in
# pretix, but we won't show wrong numbers!
return '{} {}'.format(
arg,
floatformat(value, 2)
)
return format_currency(value, arg, locale=locale) return format_currency(value, arg, locale=locale)
except: except:
return '{} {}'.format( return '{} {}'.format(arg, floatformat(value, places))
arg,
floatformat(value, places)
)
@register.filter("money_numberfield") @register.filter("money_numberfield")