-1

我有一个金额对象,我想将它分成十万和千。因此,例如说amount = 2,50,000我想将其拆分为lakhs = 2,00,000and 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)

这段代码看起来很难看,我相信有更好更短的方法。你能帮助我以更好的方式实现我想要的吗?

4

1 回答 1

3

如果我正确阅读了您的问题,您为什么不使用模数运算符?

amount = 250000

thousands = amount % 100000
lakhs = amount - thousands
于 2012-12-15T09:10:26.523 回答