From ef8563e4926491e1d75bbfe8b6a334d90111f78b Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Mon, 9 Dec 2024 15:19:38 +0000 Subject: [PATCH] Allow currency to accept strings or numbers --- app/lib/template_filters.py | 8 +++++--- test/lib/test_template_filters.py | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/lib/template_filters.py b/app/lib/template_filters.py index 9093a0a..d5ecc62 100644 --- a/app/lib/template_filters.py +++ b/app/lib/template_filters.py @@ -97,9 +97,11 @@ def pretty_date_with_day(s): def currency(s): - if int(s) == float(s): - return str(int(s)) - return str("%.2f" % s) + float_number = float(s) + int_number = int(float_number) + if int_number == float_number: + return str(int_number) + return str("%.2f" % float_number) def rfc_822_format(s): diff --git a/test/lib/test_template_filters.py b/test/lib/test_template_filters.py index bea2e12..9ac9025 100644 --- a/test/lib/test_template_filters.py +++ b/test/lib/test_template_filters.py @@ -37,3 +37,12 @@ def test_currency(self): self.assertEqual(currency(5.001), "5.00") self.assertEqual(currency(5.005), "5.00") self.assertEqual(currency(5.006), "5.01") + self.assertEqual(currency("0"), "0") + self.assertEqual(currency("5"), "5") + self.assertEqual(currency("5.0"), "5") + self.assertEqual(currency("5.00"), "5") + self.assertEqual(currency("5.1"), "5.10") + self.assertEqual(currency("5.01"), "5.01") + self.assertEqual(currency("5.001"), "5.00") + self.assertEqual(currency("5.005"), "5.00") + self.assertEqual(currency("5.006"), "5.01")