在Python中将金额转换为印度表示法 [英] Convert an amount to Indian Notation in Python

查看:187
本文介绍了在Python中将金额转换为印度表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我需要将金额转换为印度货币格式



我的代码 Python 执行:

  import decimal 
def currencyInIndiaFormat n):
d = decimal.Decimal(str(n))
如果d.as_tuple()。exponent< -2:
s = str(n)
else:
s ='{0:.2f}'。格式(n)
l = len(s)
i = L-1;
res =''
flag = 0
k = 0
while i> = 0:
if flag == 0:
res = res + s [i]
if s [i] =='。':
flag = 1
elif flag == 1:
k = k + 1
res = res + s [i]
如果k == 3且i-1> = 0:
res = res +','
flag = 2
k = 0
否则:
k = k + 1
res = res + s [i]
如果k == 2且i-1> = 0:
res = res +',
flag = 2
k = 0
i = i - 1

return res [:: - 1]

def main():
n = 100.52
printINR+ currencyInIndiaFormat(n)#INR 100.52
n = 1000.108
printINR+ currencyInIndiaFormat(n)#INR 1,000.108
n = 1200000
printINR+ currencyInIndiaFormat(n)#INR 12,00,000.00

main()

我的问题:有没有一种方法可以使我的 currencyInIndiaFormat 功能更短,更简洁和干净? /有没有更好的方法来写我的currencyInIndiaFormat函数?/ b>

注意:我的问题主要是基于 code> Python 执行上述问题。



例如,这里的数字表示为:

  1 
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00, 00,000

请参阅

太多的工作。

 >>>导入区域设置
>>> locale.setlocale(locale.LC_MONETARY,'en_IN')
'en_IN'
>>> print locale.currency(100.52,grouping = True)
$ 100.52
>>> print locale.currency(1000.108,grouping = True)
$ 1,000.11
>>> print locale.currency(1200000,grouping = True)
$ 12,00,000.00


Problem: I need to convert an amount to Indian currency format

My code: I have the following Python implementation:

import decimal
def currencyInIndiaFormat(n):
  d = decimal.Decimal(str(n))
  if d.as_tuple().exponent < -2:
    s = str(n)
  else:
    s = '{0:.2f}'.format(n)
  l = len(s)
  i = l-1;
  res = ''
  flag = 0
  k = 0
  while i>=0:
    if flag==0:
      res = res + s[i]
      if s[i]=='.':
        flag = 1
    elif flag==1:
      k = k + 1
      res = res + s[i]
      if k==3 and i-1>=0:
        res = res + ','
        flag = 2
        k = 0
    else:
      k = k + 1
      res = res + s[i]
      if k==2 and i-1>=0:
        res = res + ','
        flag = 2
        k = 0
    i = i - 1

  return res[::-1]

def main():
  n = 100.52
  print "INR " + currencyInIndiaFormat(n)  # INR 100.52
  n = 1000.108
  print "INR " + currencyInIndiaFormat(n)  # INR 1,000.108
  n = 1200000
  print "INR " + currencyInIndiaFormat(n)  # INR 12,00,000.00

main()

My Question: Is there a way to make my currencyInIndiaFormat function shorter, more concise and clean ? / Is there a better way to write my currencyInIndiaFormat function ?

Note: My question is mainly based on Python implementation of the above stated problem. It is not a duplicate of previously asked questions regarding conversion of currency to Indian format.

Indian Currency Format:

For example, numbers here are represented as:

1
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00,00,000

Refer Indian Numbering System

解决方案

Too much work.

>>> import locale
>>> locale.setlocale(locale.LC_MONETARY, 'en_IN')
'en_IN'
>>> print locale.currency(100.52, grouping=True)
₹ 100.52
>>> print locale.currency(1000.108, grouping=True)
₹ 1,000.11
>>> print locale.currency(1200000, grouping=True)
₹ 12,00,000.00

这篇关于在Python中将金额转换为印度表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆