| | |
convert amount to words
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 129
Reputation:
Solved Threads: 11
0
#2 Nov 2nd, 2009
•
•
•
•
hi,
can any one tell me how to i convert amount(in digitts) to word.
thanks in advance
You can try the following code:
C# Syntax (Toggle Plain Text)
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
•
•
Join Date: Oct 2009
Posts: 22
Reputation:
Solved Threads: 1
try this code
C# Syntax (Toggle Plain Text)
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(); }
0
#5 Nov 2nd, 2009
Although the layout could be better, this should do the trick.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
0
#8 Nov 2nd, 2009
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:
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.
C# Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Convert words into numbers (C#)
- displaying a limited amount of words from database (PHP)
- c++ convert numbers to words (C++)
- Program to convert numbers into words in visual basic 2008 (VB.NET)
- Counting the amount of different words with vectors? (C++)
Other Threads in the C# Forum
- Previous Thread: how to embed SCPI and GPIB type commands
- Next Thread: Binding data from dateTimePicker to a variable
Views: 548 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagridview dataset datetime degrees draganddrop drawing encryption enum excel file files form format forms ftp function gcd gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






