Skip to content

Commit

Permalink
Allow currency to accept strings or numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Dec 9, 2024
1 parent c3ccd0c commit ef8563e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/lib/template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions test/lib/test_template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit ef8563e

Please sign in to comment.