python - 如何使用 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
解决方案
您可以使用num2words
Python 包。它将数字转换为单词。
https://pypi.org/project/num2words/
此代码会将数字转换为印度格式的单词。
from num2words import num2words
print(num2words(100000, lang='en_IN'))
Output:
one lakh
推荐阅读
- sql - ORA-30926: 运行合并查询时无法在源表中获得一组稳定的行
- python - 如何从 pysnmp 访问 SNMP 陷阱变量,以便将它们用于 API 发布?
- django - 'QuerySet' 对象没有属性'name' Django
- python - 从 Django ORM 中高效提取大量数据
- java - 存储多个点(浮点数,浮点数)的最佳方法是什么?
- javascript - reactjs - 如何在没有提交按钮的情况下上传文件
- javascript - 执行路由更改功能(使用 HookRouter )
- python - 我的 python 代码没有返回值。请查看代码
- amazon-web-services - Terraform 路由表与 VPC 端点的关联
- c++ - 如何强制使用库的应用程序在 Windows 上包含特定的应用程序清单设置