Convert amount to word in Python In Indian Format

后端 未结 2 913
离开以前
离开以前 2021-01-29 05:02

How to convert amount to word in Indian?

I was using num2words library but its is presenting wrong set of words while presenting \'lakhs\' and \'crores\'.

For ex

2条回答
  •  难免孤独
    2021-01-29 05:47

    I guess you could just cast it on the first line if you're not handling decimals. You can also use // to do integer division.

    import decimal    
    
    def num2words(num):
        num = decimal.Decimal(num)
        decimal_part = num - int(num)
        num = int(num)
    
        if decimal_part:
            return num2words(num) + " point " + (" ".join(num2words(i) for i in str(decimal_part)[2:]))
    
        under_20 = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
        tens = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
        above_100 = {100: 'Hundred', 1000: 'Thousand', 100000: 'Lakhs', 10000000: 'Crores'}
    
        if num < 20:
            return under_20[num]
    
        if num < 100:
            return tens[num // 10 - 2] + ('' if num % 10 == 0 else ' ' + under_20[num % 10])
    
        # find the appropriate pivot - 'Million' in 3,603,550, or 'Thousand' in 603,550
        pivot = max([key for key in above_100.keys() if key <= num])
    
        return num2words(num // pivot) + ' ' + above_100[pivot] + ('' if num % pivot==0 else ' ' + num2words(num % pivot))
    
    
    print(num2words(decimal.Decimal("650958.32")))
    # Six Lakhs Fifty Thousand Nine Hundred Fifty Eight point Three Two
    

提交回复
热议问题