Convert an amount to Indian Notation in Python

前端 未结 5 710
离开以前
离开以前 2021-01-02 03:40

Problem: I need to convert an amount to Indian currency format

My code: I have the following Python implementation:

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 04:35

    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)
    

    This code will Convert Yout Numbers to Lakhs, Crores and arabs in most simplest way. Hope it helps.

    for i in [1.234567899 * 10**x for x in range(9)]:
    print(format_indian(int(i)))
    

    Output:

    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
    

提交回复
热议问题