Browse Source

fixes in fmt_money

version-14
Nabin Hait 12 years ago
parent
commit
c86c459718
1 changed files with 66 additions and 29 deletions
  1. +66
    -29
      webnotes/utils/__init__.py

+ 66
- 29
webnotes/utils/__init__.py View File

@@ -407,39 +407,76 @@ def parse_val(v):
return v return v
# ============================================================================== # ==============================================================================

def fmt_money(amount, decimal_fmt="%02.d"):
#
# def fmt_money(amount, decimal_fmt="%02.d"):
# """
# Convert to string with commas for thousands, millions etc
# """
# currency_format = webnotes.conn.get_value('Control Panel', None, 'currency_format') \
# or 'Millions'
# comma_frequency = (currency_format == "Millions") and 3 or 2
# minus = flt(amount) < 0 and "-" or ""
#
# # get whole and fraction parts
# str_amount = cstr(amount)
# if "." in str_amount:
# amount_whole = abs(cint(str_amount.split(".")[0]))
# amount_fraction = cint(str_amount.split(".")[1])
# else:
# amount_whole = abs(cint(str_amount))
# amount_fraction = 0
#
# # reverse the whole part, so that it is easy to append commas
# whole_reversed = cstr(amount_whole)[::-1]
# whole_with_comma = whole_reversed[:3]
# if len(whole_reversed) > 3:
# remaining_str = whole_reversed[3:]
# for i in xrange(len(remaining_str)):
# # insert a comma after the specified frequency of digits
# if i%comma_frequency==0:
# whole_with_comma += ","
# whole_with_comma += remaining_str[i]
# # reverse the whole part again to get the original number back!
# whole_with_comma = whole_with_comma[::-1]
# webnotes.msgprint(decimal_fmt)
# webnotes.msgprint(("%s%s."+decimal_fmt) % (minus, whole_with_comma, amount_fraction))
# return ("%s%s."+decimal_fmt) % (minus, whole_with_comma, amount_fraction)

def fmt_money(amount, precision=2):
""" """
Convert to string with commas for thousands, millions etc Convert to string with commas for thousands, millions etc
""" """
currency_format = webnotes.conn.get_value('Control Panel', None, 'currency_format') \
or 'Millions'
comma_frequency = (currency_format == "Millions") and 3 or 2
minus = flt(amount) < 0 and "-" or ""
curr = webnotes.conn.get_value('Control Panel', None, 'currency_format') or 'Millions'
amount = '%.*f' % (precision, flt(amount))
val = 2
if curr == 'Millions': val = 3


# get whole and fraction parts
str_amount = cstr(amount)
if "." in str_amount:
amount_whole = abs(cint(str_amount.split(".")[0]))
amount_fraction = cint(str_amount.split(".")[1])
else:
amount_whole = abs(cint(str_amount))
amount_fraction = 0
# reverse the whole part, so that it is easy to append commas
whole_reversed = cstr(amount_whole)[::-1]
whole_with_comma = whole_reversed[:3]
if len(whole_reversed) > 3:
remaining_str = whole_reversed[3:]
for i in xrange(len(remaining_str)):
# insert a comma after the specified frequency of digits
if i%comma_frequency==0:
whole_with_comma += ","
whole_with_comma += remaining_str[i]
# reverse the whole part again to get the original number back!
whole_with_comma = whole_with_comma[::-1]

return ("%s%s."+decimal_fmt) % (minus, whole_with_comma, amount_fraction)
if amount.find('.') == -1: temp = '00'
else: temp = amount.split('.')[1]

l = []
minus = ''
if flt(amount) < 0: minus = '-'

amount = ''.join(amount.split(','))
amount = cstr(abs(flt(amount))).split('.')[0]
# main logic
if len(amount) > 3:
nn = amount[len(amount)-3:]
l.append(nn)
amount = amount[0:len(amount)-3]
while len(amount) > val:
nn = amount[len(amount)-val:]
l.insert(0,nn)
amount = amount[0:len(amount)-val]
if len(amount) > 0: l.insert(0,amount)

amount = ','.join(l)+'.'+temp
amount = minus + amount
return amount


# #
# convet currency to words # convet currency to words


Loading…
Cancel
Save