Adjust "render_currency" behaviour (#8017)

* Adjust "render_currency" behaviour

- Fixes https://github.com/inventree/InvenTree/issues/7997

* Update docs
This commit is contained in:
Oliver 2024-08-29 12:21:38 +10:00 committed by GitHub
parent 99e822213d
commit 9afc6cc6cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 14 deletions

View File

@ -130,8 +130,8 @@ The following keyword arguments are available to the `render_currency` function:
| --- | --- |
| currency | Specify the currency code to render in (will attempt conversion if different to provided currency) |
| decimal_places | Specify the number of decimal places to render |
| min_decimal_places | Specify the minimum number of decimal places to render (ignored if *decimal_places* is specified) |
| max_decimal_places | Specify the maximum number of decimal places to render (ignored if *decimal_places* is specified) |
| min_decimal_places | Specify the minimum number of decimal places to render |
| max_decimal_places | Specify the maximum number of decimal places to render |
| include_symbol | Include currency symbol in rendered value (default = True) |
## Maths Operations

View File

@ -210,9 +210,6 @@ def render_currency(
except Exception:
pass
if decimal_places is None:
decimal_places = get_global_setting('PRICING_DECIMAL_PLACES', 6)
if min_decimal_places is None:
min_decimal_places = get_global_setting('PRICING_DECIMAL_PLACES_MIN', 0)
@ -222,17 +219,19 @@ def render_currency(
value = Decimal(str(money.amount)).normalize()
value = str(value)
if '.' in value:
decimals = len(value.split('.')[-1])
decimals = max(decimals, min_decimal_places)
decimals = min(decimals, decimal_places)
decimal_places = decimals
if decimal_places is not None:
# Decimal place count is provided, use it
pass
elif '.' in value:
# If the value has a decimal point, use the number of decimal places in the value
decimal_places = len(value.split('.')[-1])
else:
decimal_places = max(decimal_places, 2)
# No decimal point, use 2 as a default
decimal_places = 2
decimal_places = max(decimal_places, max_decimal_places)
# Clip the decimal places to the specified range
decimal_places = max(decimal_places, min_decimal_places)
decimal_places = min(decimal_places, max_decimal_places)
return format_money(
money, decimal_places=decimal_places, include_symbol=include_symbol