diff --git a/frappe/tests/test_fmt_money.py b/frappe/tests/test_fmt_money.py index 4fc8877d97..0c838bbd1c 100644 --- a/frappe/tests/test_fmt_money.py +++ b/frappe/tests/test_fmt_money.py @@ -80,8 +80,8 @@ class TestFmtMoney(unittest.TestCase): self.assertEqual(fmt_money(100000.1234), "100,000.1234") self.assertEqual(fmt_money(1000000.3456), "1,000,000.3456") self.assertEqual(fmt_money(10000000.3344567), "10,000,000.3345") - self.assertEqual(fmt_money(100000000.37827268), "100,000,000.378") - self.assertEqual(fmt_money(1000000000.2718272637), "1,000,000,000.27") + self.assertEqual(fmt_money(100000000.37827268), "100,000,000.3783") + self.assertEqual(fmt_money(1000000000.2718272637), "1,000,000,000.2718") frappe.db.set_default("currency_precision", "") if __name__=="__main__": diff --git a/frappe/utils/data.py b/frappe/utils/data.py index d8ca8c16ef..2f73c37ec5 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -373,8 +373,10 @@ def fmt_money(amount, precision=None, currency=None): # 40,000.23000 -> 40,000.23 if decimal_str: - parts = str(amount).split(decimal_str) - decimals = parts[1] if len(parts) > 1 else '' + decimals_after = str(round(amount % 1, precision)) + parts = decimals_after.split(decimal_str) + parts = parts[1] if len(parts) > 1 else '' + decimals = parts if precision > 2: if len(decimals) < 3: if currency: @@ -386,6 +388,7 @@ def fmt_money(amount, precision=None, currency=None): precision = len(decimals) amount = '%.*f' % (precision, round(flt(amount), precision)) + if amount.find('.') == -1: decimals = '' else: @@ -413,7 +416,8 @@ def fmt_money(amount, precision=None, currency=None): parts.reverse() amount = comma_str.join(parts) + ((precision and decimal_str) and (decimal_str + decimals) or "") - amount = minus + amount + if amount != '0': + amount = minus + amount if currency and frappe.defaults.get_global_default("hide_currency_symbol") != "Yes": symbol = frappe.db.get_value("Currency", currency, "symbol") or currency