首页 > 解决方案 > 如何使用 python 获取印度格式的货币?

问题描述

我有以下金额:

1000
1234
123400
1900000

我怎样才能将这些转换为:

1 Thousand
1.2 Thousand
1.2 Lakhs
19 Lakhs

我尝试了这个链接中的一个函数Convert a amount to Indian Notation in Python

def format_indian(t):
  dic = {
      4:'Thousand',
      5:'Lakh',
      6:'Lakh',
      7:'Crore',
      8:'Crore',
      9:'Arab'
  }
  y = 10
  len_of_number = len(str(t))
  save = t
  z=y
  while(t!=0):
    t=int(t/y)
    z*=10

  zeros = len(str(z)) - 3
  if zeros>3:
      if zeros%2!=0:
          string = str(save)+": "+str(save/(z/100))[0:4]+" "+dic[zeros]
      else:   
        string = str(save)+": "+str(save/(z/1000))[0:4]+" "+dic[zeros]
        return string
  return str(save)+": "+str(save)

但这给了我:

format_indian(100001)
>>> '100001: 100001'

我怎样才能做到:1 lakhs,上述解决方案仅适用于每个类别的 10 个。例如:10 Thousand, 10 Lakhs, 10 crore

标签: python

解决方案


您可以使用num2wordsPython 包。它将数字转换为单词。

https://pypi.org/project/num2words/

此代码会将数字转换为印度格式的单词。

from num2words import num2words

print(num2words(100000, lang='en_IN'))
Output:

one lakh

推荐阅读