From 159a20e5f839dacf973cbd70a97af786aaa647e8 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Sat, 31 Mar 2018 23:59:10 +0530 Subject: [PATCH 1/6] fix fmt_moeny --- frappe/tests/test_fmt_money.py | 2 +- frappe/utils/data.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/frappe/tests/test_fmt_money.py b/frappe/tests/test_fmt_money.py index 4fc8877d97..b5ede0f2b3 100644 --- a/frappe/tests/test_fmt_money.py +++ b/frappe/tests/test_fmt_money.py @@ -80,7 +80,7 @@ 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(100000000.37827268), "100,000,000.3783") self.assertEqual(fmt_money(1000000000.2718272637), "1,000,000,000.27") frappe.db.set_default("currency_precision", "") diff --git a/frappe/utils/data.py b/frappe/utils/data.py index d8ca8c16ef..acbe86f030 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -386,6 +386,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: From b083baa443c74728614b8ea42cc0710f22bfc146 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Sun, 1 Apr 2018 00:04:39 +0530 Subject: [PATCH 2/6] fix fmt_moeny --- frappe/tests/test_fmt_money.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/tests/test_fmt_money.py b/frappe/tests/test_fmt_money.py index b5ede0f2b3..0c838bbd1c 100644 --- a/frappe/tests/test_fmt_money.py +++ b/frappe/tests/test_fmt_money.py @@ -81,7 +81,7 @@ class TestFmtMoney(unittest.TestCase): 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.3783") - self.assertEqual(fmt_money(1000000000.2718272637), "1,000,000,000.27") + self.assertEqual(fmt_money(1000000000.2718272637), "1,000,000,000.2718") frappe.db.set_default("currency_precision", "") if __name__=="__main__": From 2396b105fce91f49cec9e665977bc00d625bf71d Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Sun, 1 Apr 2018 00:31:37 +0530 Subject: [PATCH 3/6] fix fmt_moeny --- frappe/utils/data.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frappe/utils/data.py b/frappe/utils/data.py index acbe86f030..78905b54b3 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -373,8 +373,7 @@ 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 = str(amount % 1).split(decimal_str)[1] if precision > 2: if len(decimals) < 3: if currency: From 701d59b5ae0b5edde292d43bbdef73cc80e7fea2 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Sun, 1 Apr 2018 00:44:31 +0530 Subject: [PATCH 4/6] fix decimal split --- frappe/utils/data.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/frappe/utils/data.py b/frappe/utils/data.py index acbe86f030..d3ae851ad0 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -373,8 +373,11 @@ 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(amount % 1) + parts = decimals_after.split(decimal_str) + parts = parts[1] if len(parts) > 1 else '' + decimals = parts + print("Decimals: {}".format(decimals)) if precision > 2: if len(decimals) < 3: if currency: @@ -414,7 +417,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 From 6c7ea223294b8d688824cf9b2ed47e166a428595 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Sun, 1 Apr 2018 01:06:20 +0530 Subject: [PATCH 5/6] fix fmt_moeny --- frappe/utils/data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/utils/data.py b/frappe/utils/data.py index e97d7764bc..55f9b9929b 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -384,7 +384,7 @@ def fmt_money(amount, precision=None, currency=None): precision = len(cstr(fraction)) - 1 else: precision = number_format_precision - elif len(decimals) < precision: + elif len(decimals) <= precision: precision = len(decimals) amount = '%.*f' % (precision, round(flt(amount), precision)) From cd13493952d082659c287f5f877f039c07c01259 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Sun, 1 Apr 2018 01:21:49 +0530 Subject: [PATCH 6/6] fix fmt_moeny --- frappe/utils/data.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frappe/utils/data.py b/frappe/utils/data.py index 55f9b9929b..2f73c37ec5 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -373,7 +373,7 @@ def fmt_money(amount, precision=None, currency=None): # 40,000.23000 -> 40,000.23 if decimal_str: - decimals_after = str(amount % 1) + decimals_after = str(round(amount % 1, precision)) parts = decimals_after.split(decimal_str) parts = parts[1] if len(parts) > 1 else '' decimals = parts @@ -384,7 +384,7 @@ def fmt_money(amount, precision=None, currency=None): precision = len(cstr(fraction)) - 1 else: precision = number_format_precision - elif len(decimals) <= precision: + elif len(decimals) < precision: precision = len(decimals) amount = '%.*f' % (precision, round(flt(amount), precision))