abhi
abhi

Reputation: 377

How to get currency in Indian format using python?

I have following amounts:

1000
1234
123400
1900000

How can i convert these to:

1 Thousand
1.2 Thousand
1.2 Lakhs
19 Lakhs

I tried a function from this link Convert an amount to Indian Notation in Python.

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)

But this is giving me:

format_indian(100001)
>>> '100001: 100001'

How can I make that to:1 lakhs, the above solution only works for 10's of every category. For example: 10 Thousand, 10 Lakhs, 10 crore

Upvotes: 2

Views: 1023

Answers (4)

heretolearn
heretolearn

Reputation: 6555

Just building up on your existing code, you can try something like this if you want the 1st part to still show numbers, else @Ram answer should work fine.

import math
def format_indian(t):
    num_count = {
        3 : 'Hundred',
        4 : 'Thousand',
        5 : 'Thousand',
        6 : 'Lakh',
        7 : 'Lakh',
        8 : 'Crore',
        9 : 'Crore',
        10: 'Arab'
    }
    num_len = len(str(t))
    power = num_len - 1 if (num_len % 2 == 0 or num_len == 3) else num_len - 2
    dig = t / math.pow(10, power)
    print("{} {}".format(round(dig,1), num_count[num_len]))

You can adjust the round-off value based on the precision you want.

Upvotes: 0

Nube Colectiva
Nube Colectiva

Reputation: 292

Just like Ram said, you can use the package: https://pypi.org/project/num2words/ and you can use a dictionary and try except

num2words = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', \
             6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten', \
            11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', \
            15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen', \
            19: 'Nineteen', 20: 'Twenty', 30: 'Thirty', 40: 'Forty', \
            50: 'Fifty', 60: 'Sixty', 70: 'Seventy', 80: 'Eighty', \
            90: 'Ninety', 0: 'Zero'}

>>> def n2w(n):
        try:
            print num2words[n]
        except KeyError:
            try:
                print num2words[n-n%10] + num2words[n%10].lower()
            except KeyError:
                print 'Number out of range'

>>> n2w(0)
Zero
>>> n2w(13)
Thirteen        
>>> n2w(91)
Ninetyone
>>> n2w(21)
Twentyone
>>> n2w(33)
Thirtythree

Upvotes: 1

Ram
Ram

Reputation: 4779

You can use num2words package of Python. It converts numbers to words.

https://pypi.org/project/num2words/

This code will convert number to words in Indian format.

from num2words import num2words

print(num2words(100000, lang='en_IN'))
Output:

one lakh

Upvotes: 2

Amit Gupta
Amit Gupta

Reputation: 2938

!pip install inflect

Not in the Indian Currency but you can get it in strings

import inflect
p = inflect.engine()

so p.number_to_words(1900000)

will give you

one million, nine hundred thousand

but if you only need in Indian Currency then you can modify 3rd answer of this SO thread

Upvotes: 0

Related Questions