diff --git a/docs/docs/report/helpers.md b/docs/docs/report/helpers.md index 79ae0c933e..abff09514e 100644 --- a/docs/docs/report/helpers.md +++ b/docs/docs/report/helpers.md @@ -320,15 +320,86 @@ The helper function `format_number` allows for some common number formatting opt show_docstring_description: false show_source: False -#### Example +#### Examples ```html {% raw %} {% load report %} -{% format_number 3.14159265359 decimal_places=5, leading=3 %} - + + +{% format_number 3.14159265359 decimal_places=5 %} + + + +{% format_number 3.14159265359 decimal_places=5 min_digits=3 %} + + + {% format_number 3.14159265359 integer=True %} + + +{% format_number 9988776.5 decimal_places=2 separator=True %} + + + +{% format_number 9988776.5 decimal_places=2 separator=True locale='de-de' %} + + + +{% format_number 0.175 multiplier=100 decimal_places=1 %} + + + +{% format_number 1234.5 decimal_places=2 max_decimal_places=6 %} + + +{% endraw %} +``` + +#### Custom Format Strings + +The `fmt` argument accepts a [Unicode number pattern](https://unicode.org/reports/tr35/tr35-numbers.html#Number_Format_Patterns) string (the same syntax used by [Babel](https://babel.pocoo.org/en/latest/numbers.html)). When `fmt` is provided it takes complete priority over the `decimal_places`, `max_decimal_places`, `min_digits`, and `separator` arguments — those arguments are silently ignored. + +The `integer` and `multiplier` arguments **are** still applied to the number before the format string is used. + +| Symbol | Meaning | +| --- | --- | +| `0` | Required digit — always rendered, even if zero | +| `#` | Optional digit — suppressed when not significant | +| `,` | Grouping separator (position defines group size) | +| `.` | Decimal separator | + +Common patterns: + +| Pattern | Example output | +| --- | --- | +| `0` | `1235` | +| `#,##0` | `1,235` | +| `0.00` | `1234.57` | +| `#,##0.00` | `1,234.57` | +| `000` | `007` | + +```html +{% raw %} +{% load report %} + + +{% format_number 1234.5678 fmt='0.00' %} + + + +{% format_number 1234.5678 fmt='#,##0.00' %} + + + +{% format_number 1234.5678 fmt='#,##0.00' locale='de-de' %} + + + +{% format_number 9988776655.4321 fmt='#,##0' integer=True %} + + {% endraw %} ``` diff --git a/src/backend/InvenTree/report/test_tags.py b/src/backend/InvenTree/report/test_tags.py index 53c87bded0..5b2ade0362 100644 --- a/src/backend/InvenTree/report/test_tags.py +++ b/src/backend/InvenTree/report/test_tags.py @@ -384,6 +384,22 @@ class ReportTagTest(PartImageTestMixin, InvenTreeTestCase): self.assertEqual(fn(123, min_digits=5), '00123') self.assertEqual(fn(1234, min_digits=2, decimal_places=4), '1234.0000') + # Test with custom 'fmt' format string (takes priority over decimal_places / separator) + self.assertEqual(fn(1234.5678, fmt='0.00'), '1234.57') + self.assertEqual(fn(1234.5678, fmt='#,##0.00'), '1,234.57') + self.assertEqual(fn(1234.5678, fmt='0.00', locale='de-de'), '1234,57') + self.assertEqual(fn(1234.5678, fmt='#,##0.00', locale='de-de'), '1.234,57') + # fmt bypasses decimal_places and separator options + self.assertEqual( + fn(1234.5678, fmt='0.00', decimal_places=4, separator=True), '1234.57' + ) + # integer conversion still applies before fmt is used + self.assertEqual( + fn(9988776655.4321, fmt='#,##0', integer=True), '9,988,776,655' + ) + # multiplier is applied before fmt is used + self.assertEqual(fn(100, fmt='0.00', multiplier=1.5), '150.00') + # Test with multiplier self.assertEqual(fn(1000, multiplier=1.5), '1500')