diff --git a/public/js/legacy/utils/datatype.js b/public/js/legacy/utils/datatype.js index e729f6c9e8..f6539c4c84 100644 --- a/public/js/legacy/utils/datatype.js +++ b/public/js/legacy/utils/datatype.js @@ -29,7 +29,11 @@ function fmt_money(v, format){ } function format_currency(v, currency) { - return get_currency_symbol(currency) + " " + format_number(v); + var format = get_number_format() + if(locals["Currency"][currency] + && locals["Currency"][currency].number_format) + format = locals["Currency"][currency].number_format; + return get_currency_symbol(currency) + " " + format_number(v, format); } function get_currency_symbol(currency) { @@ -61,20 +65,13 @@ function get_number_format() { return global_number_format; } -var global_number_format_info = null -function get_number_format_info(format) { - if(!global_number_format_info) { - if(!format) - format = get_number_format(); - var result = format.match(/[^\d\-\+#]/g); - var info = { - decimal_str: (result && result[result.length-1]) || '.', //treat the right most symbol as decimal - group_sep: (result && result[1] && result[0]) || ',' //treat the left most symbol as group separator - } - info.precision = format.split(info.decimal_str)[1].length; - global_number_format_info = info; - } - return global_number_format_info +var number_format_info = { + "#,###.##": {decimal_str:".", group_sep:",", precision:2}, + "#.###,##": {decimal_str:",", group_sep:".", precision:2}, + "# ###.##": {decimal_str:".", group_sep:" ", precision:2}, + "#,##,###.##": {decimal_str:".", group_sep:",", precision:2}, + "#.###": {decimal_str:"", group_sep:".", precision:0}, + "#,###": {decimal_str:"", group_sep:",", precision:0}, } // to title case diff --git a/public/js/lib/number_formatter.js b/public/js/lib/number_formatter.js index ddedaf6a29..01612a8f81 100644 --- a/public/js/lib/number_formatter.js +++ b/public/js/lib/number_formatter.js @@ -16,7 +16,10 @@ window['format_number'] = function(v, m, decimals){ if (!m) { m = get_number_format(); } - var info = get_number_format_info(m); + var info = number_format_info[m]; + if(!info) { + info = {decimal_str:".", group_sep:",", precision:2}; + } if(isNaN(+v)) { v=0; diff --git a/webnotes/utils/__init__.py b/webnotes/utils/__init__.py index f8d515039c..06b68fa63c 100644 --- a/webnotes/utils/__init__.py +++ b/webnotes/utils/__init__.py @@ -373,23 +373,16 @@ def fmt_money(amount, precision=None): curr = webnotes.conn.get_value('Control Panel', None, 'currency_format') or 'Millions' number_format = webnotes.conn.get_default("number_format") or "#,###.##" - - breaks = re.findall('[^#\d]+', number_format) - if len(breaks) < 2: - webnotes.msgprint(_("Incorrect Number Format:") + number_format, - raise_exception=True) - - decimal_str = breaks[-1] - comma_str = breaks[0] - if not precision: - precision = len(number_format.split(decimal_str)[-1]) or 2 - - amount = '%.*f' % (precision, flt(amount)) + decimal_str, comma_str, precision = get_number_format_info(number_format) val = 2 if curr == 'Millions': val = 3 + + amount = '%.*f' % (precision, flt(amount)) - if amount.find('.') == -1: temp = '00' - else: temp = amount.split('.')[1] + if amount.find('.') == -1: + decimals = '' + else: + decimals = amount.split('.')[1] l = [] minus = '' @@ -409,9 +402,23 @@ def fmt_money(amount, precision=None): if len(amount) > 0: l.insert(0,amount) - amount = comma_str.join(l) + decimal_str + temp + amount = comma_str.join(l) + decimal_str + decimals amount = minus + amount return amount + +def get_number_format_info(format): + if format=="#.###": + return "", ".", 0 + elif format=="#,###": + return "", ",", 0 + elif format=="#,###.##" or format=="#,##,###.##": + return ".", ",", 2 + elif format=="#.###,##": + return ",", ".", 2 + elif format=="# ###.##": + return ".", " ", 2 + else: + return ".", ",", 2 # # convet currency to words