954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

convert amount to words

hi,
can any one tell me how to i convert amount(in digitts) to word.
thanks in advance

kool.net
Light Poster
28 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
hi, can any one tell me how to i convert amount(in digitts) to word. thanks in advance


You can try the following code:

public  string NumberToText(int number) 
{ 
if (number == 0) return "Zero"; 
if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight"; 
int[] num = new int[4]; 
int first = 0; 
int u, h, t; 
System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
if (number < 0) 
{ 
sb.Append("Minus "); 
number = -number; 
} 
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 "}; 
num[0] = number % 1000; // units 
num[1] = number / 1000; 
num[2] = number / 100000; 
num[1] = num[1] - 100 * num[2]; // thousands 
num[3] = number / 10000000; // crores 
num[2] = num[2] - 100 * num[3]; // lakhs 
for(int i = 3; i > 0 ; i--) 
{ 
if (num[i] != 0) 
{ 
first = i; 
break; 
} 
} 
for(int i = first ; i >= 0 ; i--) 
{ 
if (num[i] == 0) continue; 
u = num[i] % 10; // ones 
t = num[i] / 10; 
h = num[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(); 
}

If it helps,then mark this thread as solved

vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 11
Solved Threads: 12
 

try this code

public  string NumberToText(int number) 
{ 
if (number == 0) return "Zero"; 
if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight"; 
int[] num = new int[4]; 
int first = 0; 
int u, h, t; 
System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
if (number < 0) 
{ 
sb.Append("Minus "); 
number = -number; 
} 
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 "}; 
num[0] = number % 1000; // units 
num[1] = number / 1000; 
num[2] = number / 100000; 
num[1] = num[1] - 100 * num[2]; // thousands 
num[3] = number / 10000000; // crores 
num[2] = num[2] - 100 * num[3]; // lakhs 
for(int i = 3; i > 0 ; i--) 
{ 
if (num[i] != 0) 
{ 
first = i; 
break; 
} 
} 
for(int i = first ; i >= 0 ; i--) 
{ 
if (num[i] == 0) continue; 
u = num[i] % 10; // ones 
t = num[i] / 10; 
h = num[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(); 
}
bk_bhupendra
Newbie Poster
22 posts since Oct 2009
Reputation Points: 8
Solved Threads: 1
 

bk_bhupendra ,I think you must make your efforts .Copying others code and pasting is wrong..............

vinnijain
Junior Poster
145 posts since Jul 2009
Reputation Points: 11
Solved Threads: 12
 

Although the layout could be better, this should do the trick.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

sry....
bt i thought it wil help u...
i tested it n thn i sent dis...

bk_bhupendra
Newbie Poster
22 posts since Oct 2009
Reputation Points: 8
Solved Threads: 1
 
hi, can any one tell me how to i convert amount(in digitts) to word. thanks in advance


using the code which vinnijain is given you, you can also try modifying the same for millions and billions also.............:)

avirag
Posting Whiz
313 posts since Jun 2009
Reputation Points: 31
Solved Threads: 36
 

You could store all the numeric semantics in a dictionary and then recursively call a translator method (as needed) that whittles it down from most significant to least signicant digits... For example:

Dictionary<uint, string> lookupWord = new Dictionary<uint, string>();

        public void Init()
        {
            lookupWord.Add(0, "siro");
            lookupWord.Add(1, "uno");
            lookupWord.Add(2, "dos");
            lookupWord.Add(3, "tres");
            lookupWord.Add(4, "kwatro");
   ...

            lookupWord.Add(800, "otsosyentos");
            lookupWord.Add(900, "nobesyentos");
            lookupWord.Add(1000, "un mil");
            lookupWord.Add(1000000, "un milyon");
            lookupWord.Add(1000000000, "un bilyon");
        }

        private string ConvertNumToText(uint n)
        {
            if (
                  (n < 20) ||
                  (n < 100 && (n % 10 == 0)) ||
                  (n < 1000 && n % 100 == 0) ||
                  (n == 1000) ||
                  (n == 1000000) ||
                  (n == 1000000000)
               )
            {
                return lookupWord[n];
            }
            else if (n < 100)
            {
                uint tens = n / 10;
                uint ones = n % 10;

                if (tens != 2)
                    return lookupWord[10 * tens] + "-i-" + lookupWord[ones];
                else
                    return lookupWord[10 * tens] + "-" + lookupWord[ones];
            }
    ...

            else if (n < 2000000000)
                return "un bilyon " + ConvertNumToText(n - 1000000000);
            else
            {
                uint billions = n / 1000000000;

                if (n % 1000000000 != 0)
                    return ConvertNumToText(billions) + " bilyon " + ConvertNumToText(n - 1000000000 * billions);
                else
                    return ConvertNumToText(billions) + " bilyon";
            }
        }


The above sample demonstrates working with the Cebuano language, but you can easily see the logic in making the proper interpretation to form a grammatical representation of a number.

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 
hi, can any one tell me how to i convert amount(in digitts) to word. thanks in advance


Well Kool.net.......
Kindly mark this thread as solved if your problem is solved now..........:)

avirag
Posting Whiz
313 posts since Jun 2009
Reputation Points: 31
Solved Threads: 36
 

hey,
how to i store value of sb in text box return from the function

kool.net
Light Poster
28 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

bk_bhupendra , the code is fine but for single digit or two digits it is showing the word "and" before the converted text. Please try to work on this.

Ramesh Kumar

kumarvarshadr
Newbie Poster
1 post since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You