我有一个Amount对象,它是一个整数,我想分成Lakhs和Thousands。因此,举例说amount = 2,50,000
我希望将其拆分为lakhs = 2,00,000
和thousands = 50,000
。
目前我正在使用以下方法。
def split_amount(value):
""" A custom method to Split amount into Lakhs and Thousands """
tho, lak = 0, 0
digits = list(str(value))
if len(digits) < 4:
print 'Error : Amount to small to split'
elif len(digits) == 4:
tho = ('').join(digits[-4:])
elif len(digits) == 5:
tho = ('').join(digits[-5:])
elif len(digits) == 6:
tho = ('').join(digits[-5:])
lak = ('').join(digits[-6:])
elif len(digits) >= 7:
tho = ('').join(digits[-5:])
lak = ('').join(digits[-7:])
else:
print "Error : Unknown Error"
if int(tho) <= 0:
thousands = 0
else:
thousands = int(tho)
if (int(lak) - int(tho) <= 0):
lakhs = 0
else:
lakhs = int(lak) - int(tho)
return (lakhs, thousands)
这段代码看起来很难看,我相信有更好更短的方法。你能帮助我以更好的方式实现我想要的东西吗?
答案 0 :(得分:3)
如果我正确地阅读了您的问题,为什么不使用模数运算符?
amount = 250000
thousands = amount % 100000
lakhs = amount - thousands