user20432746
user20432746

Reputation: 87

How to convert numerical amount in words in indian format

I have written the below script to convert any CCY to INR based on the latest exchange rate available on the following website and then convert that resulting number to words in indian numbering style. the code works perfectly it just that i am not getting the print in words in proper indian format Rs 198400 should be one lakh night eight thousand four hundred rupees only.

import requests
import num2words

def convert_ccy_to_inr(ccy_amount, ccy):
    # Get the latest exchange rate for the specified currency
    r = requests.get(f"https://api.exchangerate-api.com/v4/latest/{ccy}")
    exchange_rate = r.json()["rates"]["INR"]

    # Convert the currency amount to INR using the exchange rate
    inr_amount = ccy_amount * exchange_rate

    # Convert the INR amount to words in the Indian numbering system
    inr_amount_words = num2words.num2words(inr_amount, lang='en_IN')

    return inr_amount_words

# Accept user input for the currency amount and currency code
ccy_amount = float(input("Enter the currency amount: "))
ccy = input("Enter the currency code (e.g. USD, GBP, EUR): ")

# Convert the currency amount to INR and display the result
inr_amount_words = convert_ccy_to_inr(ccy_amount, ccy)
print(f"{ccy_amount} {ccy} is equivalent to {inr_amount_words} rupees")

Upvotes: 2

Views: 213

Answers (1)

Shounak Das
Shounak Das

Reputation: 368

You are not getting the desirable answer because num2words module is returning the words with commas(,) and decimal number. To solve this you can use these steps:

  1. use inr_rupees, inr_paisa = "{:.2f}".format(inr_amount).split(".") to round up the value upto 2 decimal value and to split it with respect to "." and assign values to inr_rupees and inr_paisa
  2. Convert the INR Rupees and Paisa amount to words and return as a list [inr_amount_words, inr_paisa_amount_words]
  3. use convert_ccy_to_inr(ccy_amount, ccy)[0] to fetch 1st element from the word list and use .replace(",", "") and .replace("and","") method to remove unwanted "and" and ",".
  4. do same for inr_paisa_amount_words (except the "and" part)

so Your final code should be:

import requests
import num2words

def convert_ccy_to_inr(ccy_amount, ccy):
    # Get the latest exchange rate for the specified currency
    r = requests.get(f"https://api.exchangerate-api.com/v4/latest/{ccy}")
    exchange_rate = r.json()["rates"]["INR"]

    # Convert the currency amount to INR using the exchange rate
    inr_amount = ccy_amount * exchange_rate
    inr_rupees, inr_paisa = "{:.2f}".format(inr_amount).split(".")
    # Convert the INR amount to words in the Indian numbering system
    inr_amount_words = num2words.num2words(inr_rupees, lang='en_IN')
    inr_paisa_amount_words = num2words.num2words(inr_paisa, lang='en_IN')

    return [inr_amount_words, inr_paisa_amount_words]

# Accept user input for the currency amount and currency code
ccy_amount = float(input("Enter the currency amount: "))
ccy = input("Enter the currency code (e.g. USD, GBP, EUR): ")

# Convert the currency amount to INR and display the result
inr_rupees_amount_words = convert_ccy_to_inr(ccy_amount, ccy)[0].replace(",","").replace("and","")
inr_paisa_amount_words = convert_ccy_to_inr(ccy_amount, ccy)[1].replace(",","")

print(f"{ccy_amount} {ccy} is equivalent to {inr_rupees_amount_words} rupees and {inr_paisa_amount_words} paisa")

example output:

Enter the currency amount: 6
Enter the currency code (e.g. USD, GBP, EUR): usd
6.0 usd is equivalent to four hundred  ninety-six rupees and thirty-eight paisa

Upvotes: 1

Related Questions