Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to convert amount in digits to wordsin English Metrics say million etc. Currently I have a code to convert in Indian Rupee format. I want in English Metrics form.
My code is as below:
C#
public static string NumbersToWords(int inputNumber)
{
    int inputNo = inputNumber;

    if (inputNo == 0)
        return "Zero";

    int[] numbers = new int[4];
    int first = 0;
    int u, h, t;
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    if (inputNo < 0)
    {
        sb.Append("Minus ");
        inputNo = -inputNo;
    }

    string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
            "Five " ,"Six ", "Seven ", "Eight ", "Nine "};
    string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
            "Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
    string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
            "Seventy ","Eighty ", "Ninety "};
    string[] words3 = { "Thousand ", "Lakh ", "Crore " };

    numbers[0] = inputNo % 1000; // units
    numbers[1] = inputNo / 1000;
    numbers[2] = inputNo / 100000;
    numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
    numbers[3] = inputNo / 10000000; // crores
    numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs

    for (int i = 3; i > 0; i--)
    {
        if (numbers[i] != 0)
        {
            first = i;
            break;
        }
    }
    for (int i = first; i >= 0; i--)
    {
        if (numbers[i] == 0) continue;
        u = numbers[i] % 10; // ones
        t = numbers[i] / 10;
        h = numbers[i] / 100; // hundreds
        t = t - 10 * h; // tens
        if (h > 0) sb.Append(words0[h] + "Hundred ");
        if (u > 0 || t > 0)
        {
            if (h > 0 || i == 0) sb.Append("and ");
            if (t == 0)
                sb.Append(words0[u]);
            else if (t == 1)
                sb.Append(words1[u]);
            else
                sb.Append(words2[t - 2] + words0[u]);
        }
        if (i != 0) sb.Append(words3[i - 1]);
    }
    return sb.ToString().TrimEnd();
}
Posted
Comments
Andy Lanng 13-May-15 6:02am    
Whats the problem?
Where are you stuck?
Kandiya 13-May-15 9:32am    
I want the same in English Metric . I mean in millions instead of crore, lakhs etc. Which is the indian Rupee format.

1 solution

Ok, I think I get you but correct me if I'm wrong.

There is no single English work for lakh (100,000) or crore (10,000,000) other that One Hundred Thousand and Ten Million

I suggest you try breaking it down by sets of 10^3 and work through the Tens and Hundreds at each 10^3 in magnitude.

Pseudo-code like this
C#
//1000 = Thousand
//1000,000 = Million
//1000,000,000 - Billion
//etc...

 units = number % 1000
 thousands = number / 1000
 tUnits = thousands % 1000
 millions = thousands / 1000
 mUnits = millions % 1000

//treat units, tUnits and mUnits the same:

 string result = "";

 if(millions>0){
  result += UnitString(mUnits)
  result += " million"
 }
 if(Thousands>0){
  result += UnitString(mUnits)
  result += " thousand"
 }
 if(number>0){
  result += UnitString(mUnits)
 }
...
  private string UnitsString(int Units){
    // deal with 100's, 10's and units
  }
...



so the numbers should turn out like this:

10,000,000 = "Ten million"

12,123,123 = "Twelve million One hundred and Twenty Three Thousand One hundred and Twenty Three"

The same pattern is true of all English (Arabic) numbers of any magnitude.

10^(3*1) = 10^3 = Thousand = 1,000
10^(3*2) = 10^6 = Million = 1,000,000
10^(3*3) = 10^9 = Billion = 1,000,000,000
10^(3*4) = 10^12 = Trillion = 1,000,000,000,000
10^(3*5) = 10^15 = Quadrillion = 1,000,000,000,000,000
10^(3*6) = 10^18 = Quintilian = 1,000,000,000,000,000,000


Hope that helps

Andy ^_^
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900