diff --git a/accounting/templates/accounting/include/record_form-non-transfer.html b/accounting/templates/accounting/include/record_form-non-transfer.html index 94adfc9..0c90998 100644 --- a/accounting/templates/accounting/include/record_form-non-transfer.html +++ b/accounting/templates/accounting/include/record_form-non-transfer.html @@ -19,6 +19,7 @@ form-record-non-transfer.html: The template for a record in the non-transfer tra Author: imacat@mail.imacat.idv.tw (imacat) First written: 2020/8/5 {% endcomment %} +{% load accounting %}
  • {% if record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}
    @@ -37,7 +38,7 @@ First written: 2020/8/5
    - +
    {{ record.amount.errors.0|default:"" }}
    diff --git a/accounting/templates/accounting/include/record_form-transfer.html b/accounting/templates/accounting/include/record_form-transfer.html index 478a229..b5e3555 100644 --- a/accounting/templates/accounting/include/record_form-transfer.html +++ b/accounting/templates/accounting/include/record_form-transfer.html @@ -19,6 +19,7 @@ form-record-transfer.html: The template for a record in the transfer transaction Author: imacat@mail.imacat.idv.tw (imacat) First written: 2020/8/5 {% endcomment %} +{% load accounting %}
  • {% if record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}
    @@ -36,7 +37,7 @@ First written: 2020/8/5
    - +
    {{ record.amount.errors.0|default:"" }}
    diff --git a/accounting/templatetags/accounting.py b/accounting/templatetags/accounting.py index 94fbebe..288ef66 100644 --- a/accounting/templatetags/accounting.py +++ b/accounting/templatetags/accounting.py @@ -32,6 +32,21 @@ from mia_core.period import Period register = template.Library() +def _strip_decimal_zeros(value: Decimal) -> str: + """Formats a decimal value, stripping excess decimal zeros. + + Args: + value: The value. + + Returns: + str: The value with excess decimal zeros stripped. + """ + s = str(value) + s = re.sub(r"^(.*\.[0-9]*?)0+$", r"\1", s) + s = re.sub(r"^(.*)\.$", r"\1", s) + return s + + def _format_positive_amount(value: Decimal) -> str: """Formats a positive amount, groups every 3 digits by commas. @@ -41,14 +56,12 @@ def _format_positive_amount(value: Decimal) -> str: Returns: str: The amount in the desired format. """ - s = str(value) + s = _strip_decimal_zeros(value) while True: m = re.match("^([1-9][0-9]*)([0-9]{3}.*)", s) if m is None: break s = m.group(1) + "," + m.group(2) - s = re.sub(r"^(.*\.[0-9]*?)0+$", r"\1", s) - s = re.sub(r"^(.*)\.$", r"\1", s) return s @@ -93,6 +106,21 @@ def short_amount(value: Optional[Decimal]) -> str: return s +@register.filter +def short_value(value: Optional[Decimal]) -> str: + """Formats a decimal value, stripping excess decimal zeros. + + Args: + value: The value. + + Returns: + str: The value with excess decimal zeroes stripped. + """ + if value is None: + return "" + return _strip_decimal_zeros(value) + + @register.simple_tag(takes_context=True) def report_url(context: RequestContext, cash_account: Optional[Account],