将金额转换为Python中的印度符号
问题:我需要将金额转换为印度货币格式 我的代码:我有以下将金额转换为Python中的印度符号,python,formatting,currency,Python,Formatting,Currency,问题:我需要将金额转换为印度货币格式 我的代码:我有以下Python实现: 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
Python
实现:
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()
请参阅工作太多
>>> 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
无法使其他两种解决方案对我起作用,所以我做了一些低技术的东西:
def format_as_indian(input):
input_list = list(str(input))
if len(input_list) <= 1:
formatted_input = input
else:
first_number = input_list.pop(0)
last_number = input_list.pop()
formatted_input = first_number + (
(''.join(l + ',' * (n % 2 == 1) for n, l in enumerate(reversed(input_list)))[::-1] + last_number)
)
if len(input_list) % 2 == 0:
formatted_input.lstrip(',')
return formatted_input
def格式为印度文(输入):
输入\列表=列表(str(输入))
如果len(input_list)您可以按照以下步骤操作。
从pip安装Babel python包
pip install Babel
在python脚本中
from babel.numbers import format_currency
format_currency(5433422.8012, 'INR', locale='en_IN')
输出:
₹ 54,33,422.80
1: 1
12: 12
123: 123
1234: 1234
12345: 12.3 Thousand
123456: 1.23 Lakh
1234567: 12.3 Lakh
12345678: 1.23 Crore
123456789: 12.3 Crore
另一种方法是:
import re
def in_for(value):
value,b=str(value),''
value=''.join(map(lambda va:va if re.match(r'[0-9,.]',va) else '',value))
val=value
if val.count(',')==0:
v,c,a,cc,ii=val,0,[3,2,2],0,0
val=val[:val.rfind('.')] if val.rfind('.')>=0 else val
for i in val[::-1]:
if c==ii and c!=0:
ii+=a[cc%3]
b=','+i+b
cc+=1
else:
b=i+b
c+=1
b=b[1:] if b[0]==',' else b
val=b+v[value.rfind('.'):] if value.rfind('.')>=0 else b
else:
val=str(val).strip('()').replace(' ','')
v=val.rfind('.')
if v>0:
val=val[:v+3]
return val.rstrip('0').rstrip('.') if '.' in val else val
print(in_for('1000000000000.5445'))
输出将为:
10,000,00,00,000.54
(如维基百科印度数字系统Ex:67,89000,00,00000所述)
这段代码将以最简单的方式将Yout数字转换为10万克朗、克罗尔和阿拉伯人。希望能有帮助
for i in [1.234567899 * 10**x for x in range(9)]:
print(format_indian(int(i)))
输出:
₹ 54,33,422.80
1: 1
12: 12
123: 123
1234: 1234
12345: 12.3 Thousand
123456: 1.23 Lakh
1234567: 12.3 Lakh
12345678: 1.23 Crore
123456789: 12.3 Crore
另一种方式:
你能告诉我们印度货币的格式吗?里面有很多单变量名称。让这些更具描述性将是最容易的胜利。另外,这应该在代码审查中。@DYZ:添加了印度货币格式!!!不适用于我,我收到以下消息错误:不支持的区域设置,然后安装区域设置,或使用类似的设置。显然,似乎无法在OSX上安装区域设置。这是可行的。但是,您需要在本地计算机中配置区域设置。在linux中,只需在终端shell中键入locale。。如果本地计算机中不存在使用的区域设置。它将抛出错误。我们如何在macOS Mojave上的locale中安装en_?这一个提供正确的输出,但比locale
慢得多。
def formatted_int(value):
# if the value is 100, 10, 1
if len(str(value)) <= 3:
return value
# if the value is 10,000, 1,000
elif 3 < len(str(value)) <= 5:
return f'{str(value)[:-3]},{str(value)[-3:]} ₹'
# if the value is greater the 10,000
else:
cut = str(value)[:-3]
o = []
while cut:
o.append(cut[-2:]) # appending from 1000th value(right to left)
cut = cut[:-2]
o = o[::-1] # reversing list
res = ",".join(o)
return f'{res},{str(value)[-3:]} ₹'
value1 = 1_00_00_00_000
value2 = 10_00_00_00_000
value3 = 100
print(formatted_int(value1))
print(formatted_int(value2))
print(formatted_int(value3))
1,00,00,00,000 ₹
10,00,00,00,000 ₹
100 ₹