Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to convert a number to Indian words format with lakhs and crores?

As the title suggests I would like to convert a long number to the format with words using C#. The Culture settings don't seem to do this and I am just currently doing this

String.Format(new CultureInfo("en-IN"), "{0:C0}", Price)

But for very long numbers I would prefer the word format. I am not from India and only vaguely familiar with how the system works.

like image 978
Craig Avatar asked Dec 29 '22 22:12

Craig


2 Answers

public static string NumberToWords(int number)
    {
        if (number == 0) { return "zero"; }
        if (number < 0) { return "minus " + NumberToWords(Math.Abs(number)); }
        string words = "";
        if ((number / 10000000) > 0) { words += NumberToWords(number / 10000000) + " Crore "; number %= 10000000; }
        if ((number / 100000) > 0) { words += NumberToWords(number / 100000) + " Lakh "; number %= 100000; }
        if ((number / 1000) > 0) { words += NumberToWords(number / 1000) + " Thousand "; number %= 1000; }
        if ((number / 100) > 0) { words += NumberToWords(number / 100) + " Hundred "; number %= 100; }
        if (number > 0)
        {
            if (words != "") { words += "and "; }
            var unitsMap = new[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
            var tensMap = new[] { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "seventy", "Eighty", "Ninety" };
            if (number < 20) { words += unitsMap[number]; }
            else { words += tensMap[number / 10]; if ((number % 10) > 0) { words += "-" + unitsMap[number % 10]; } }
        }
        return words;
    }
like image 155
SREEKANTH.KS Avatar answered Dec 31 '22 11:12

SREEKANTH.KS


  • 1 - One
  • 10 - Ten
  • 1000 - Thousand
  • 10,000 - Ten Thousand
  • 1,00,000 - Lakh
  • 10,00,000 - Ten Lakh
  • 1,00,00,000 - Crore
  • 10,00,00,000 - Ten Crore

.. after this, Arab, Kharab, Neel etc are NOT in conventional use. Infact, even financial institutions follow this:

  • 100,00,00,000 - Hundred Crore
  • 1,000,00,00,000 - One Thousand Crore
  • 10,000,00,00,000 - Ten Thousand Crore
  • 1,00,000,00,00,000 - One Lakh Crore (Trillion)
  • 99,00,000,00,00,000 - Ninety-nine Lakh Crore (Trillion)

Once this number has been exceeded, they conveniently switch to millions/billions/trillions/quadrillions..

like image 44
Ryan Fernandes Avatar answered Dec 31 '22 11:12

Ryan Fernandes