convert amount to words

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 27
Reputation: kool.net is an unknown quantity at this point 
Solved Threads: 0
kool.net kool.net is offline Offline
Light Poster

convert amount to words

 
0
  #1
Nov 2nd, 2009
hi,
can any one tell me how to i convert amount(in digitts) to word.
thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 129
Reputation: vinnijain is an unknown quantity at this point 
Solved Threads: 11
vinnijain vinnijain is offline Offline
Junior Poster
 
0
  #2
Nov 2nd, 2009
Originally Posted by kool.net View Post
hi,
can any one tell me how to i convert amount(in digitts) to word.
thanks in advance

You can try the following code:
  1. public string NumberToText(int number)
  2. {
  3. if (number == 0) return "Zero";
  4. if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight";
  5. int[] num = new int[4];
  6. int first = 0;
  7. int u, h, t;
  8. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  9. if (number < 0)
  10. {
  11. sb.Append("Minus ");
  12. number = -number;
  13. }
  14. string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
  15. "Five " ,"Six ", "Seven ", "Eight ", "Nine "};
  16. string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
  17. "Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
  18. string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
  19. "Seventy ","Eighty ", "Ninety "};
  20. string[] words3 = {"Thousand ", "Lakh ","Crore "};
  21. num[0] = number % 1000; // units
  22. num[1] = number / 1000;
  23. num[2] = number / 100000;
  24. num[1] = num[1] - 100 * num[2]; // thousands
  25. num[3] = number / 10000000; // crores
  26. num[2] = num[2] - 100 * num[3]; // lakhs
  27. for(int i = 3; i > 0 ; i--)
  28. {
  29. if (num[i] != 0)
  30. {
  31. first = i;
  32. break;
  33. }
  34. }
  35. for(int i = first ; i >= 0 ; i--)
  36. {
  37. if (num[i] == 0) continue;
  38. u = num[i] % 10; // ones
  39. t = num[i] / 10;
  40. h = num[i] / 100; // hundreds
  41. t = t - 10 * h; // tens
  42. if (h > 0) sb.Append(words0[h] + "Hundred ");
  43. if (u > 0 || t > 0)
  44. {
  45. if (h > 0 || i == 0) sb.Append("and ");
  46. if (t == 0)
  47. sb.Append(words0[u]);
  48. else if (t == 1)
  49. sb.Append(words1[u]);
  50. else
  51. sb.Append(words2[t-2] + words0[u]);
  52. }
  53. if (i != 0) sb.Append(words3[i-1]);
  54. }
  55. return sb.ToString().TrimEnd();
  56. }


If it helps,then mark this thread as solved
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: bk_bhupendra is an unknown quantity at this point 
Solved Threads: 1
bk_bhupendra bk_bhupendra is offline Offline
Newbie Poster

number to words

 
-1
  #3
Nov 2nd, 2009
try this code

  1. public string NumberToText(int number)
  2. {
  3. if (number == 0) return "Zero";
  4. if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight";
  5. int[] num = new int[4];
  6. int first = 0;
  7. int u, h, t;
  8. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  9. if (number < 0)
  10. {
  11. sb.Append("Minus ");
  12. number = -number;
  13. }
  14. string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
  15. "Five " ,"Six ", "Seven ", "Eight ", "Nine "};
  16. string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
  17. "Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
  18. string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
  19. "Seventy ","Eighty ", "Ninety "};
  20. string[] words3 = {"Thousand ", "Lakh ","Crore "};
  21. num[0] = number % 1000; // units
  22. num[1] = number / 1000;
  23. num[2] = number / 100000;
  24. num[1] = num[1] - 100 * num[2]; // thousands
  25. num[3] = number / 10000000; // crores
  26. num[2] = num[2] - 100 * num[3]; // lakhs
  27. for(int i = 3; i > 0 ; i--)
  28. {
  29. if (num[i] != 0)
  30. {
  31. first = i;
  32. break;
  33. }
  34. }
  35. for(int i = first ; i >= 0 ; i--)
  36. {
  37. if (num[i] == 0) continue;
  38. u = num[i] % 10; // ones
  39. t = num[i] / 10;
  40. h = num[i] / 100; // hundreds
  41. t = t - 10 * h; // tens
  42. if (h > 0) sb.Append(words0[h] + "Hundred ");
  43. if (u > 0 || t > 0)
  44. {
  45. if (h > 0 || i == 0) sb.Append("and ");
  46. if (t == 0)
  47. sb.Append(words0[u]);
  48. else if (t == 1)
  49. sb.Append(words1[u]);
  50. else
  51. sb.Append(words2[t-2] + words0[u]);
  52. }
  53. if (i != 0) sb.Append(words3[i-1]);
  54. }
  55. return sb.ToString().TrimEnd();
  56. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 129
Reputation: vinnijain is an unknown quantity at this point 
Solved Threads: 11
vinnijain vinnijain is offline Offline
Junior Poster
 
0
  #4
Nov 2nd, 2009
bk_bhupendra ,I think you must make your efforts .Copying others code and pasting is wrong..............
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,058
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 313
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic
 
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: bk_bhupendra is an unknown quantity at this point 
Solved Threads: 1
bk_bhupendra bk_bhupendra is offline Offline
Newbie Poster

sry

 
0
  #6
Nov 2nd, 2009
sry....
bt i thought it wil help u...
i tested it n thn i sent dis...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 237
Reputation: avirag is an unknown quantity at this point 
Solved Threads: 21
avirag avirag is offline Offline
Posting Whiz in Training
 
0
  #7
Nov 2nd, 2009
Originally Posted by kool.net View Post
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.............
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 972
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 213
DdoubleD DdoubleD is offline Offline
Posting Shark
 
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:

  1. Dictionary<uint, string> lookupWord = new Dictionary<uint, string>();
  2.  
  3. public void Init()
  4. {
  5. lookupWord.Add(0, "siro");
  6. lookupWord.Add(1, "uno");
  7. lookupWord.Add(2, "dos");
  8. lookupWord.Add(3, "tres");
  9. lookupWord.Add(4, "kwatro");
  10. ...
  11.  
  12. lookupWord.Add(800, "otsosyentos");
  13. lookupWord.Add(900, "nobesyentos");
  14. lookupWord.Add(1000, "un mil");
  15. lookupWord.Add(1000000, "un milyon");
  16. lookupWord.Add(1000000000, "un bilyon");
  17. }
  18.  
  19. private string ConvertNumToText(uint n)
  20. {
  21. if (
  22. (n < 20) ||
  23. (n < 100 && (n % 10 == 0)) ||
  24. (n < 1000 && n % 100 == 0) ||
  25. (n == 1000) ||
  26. (n == 1000000) ||
  27. (n == 1000000000)
  28. )
  29. {
  30. return lookupWord[n];
  31. }
  32. else if (n < 100)
  33. {
  34. uint tens = n / 10;
  35. uint ones = n % 10;
  36.  
  37. if (tens != 2)
  38. return lookupWord[10 * tens] + "-i-" + lookupWord[ones];
  39. else
  40. return lookupWord[10 * tens] + "-" + lookupWord[ones];
  41. }
  42. ...
  43.  
  44. else if (n < 2000000000)
  45. return "un bilyon " + ConvertNumToText(n - 1000000000);
  46. else
  47. {
  48. uint billions = n / 1000000000;
  49.  
  50. if (n % 1000000000 != 0)
  51. return ConvertNumToText(billions) + " bilyon " + ConvertNumToText(n - 1000000000 * billions);
  52. else
  53. return ConvertNumToText(billions) + " bilyon";
  54. }
  55. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 237
Reputation: avirag is an unknown quantity at this point 
Solved Threads: 21
avirag avirag is offline Offline
Posting Whiz in Training
 
1
  #9
Nov 3rd, 2009
Originally Posted by kool.net View Post
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..........
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 27
Reputation: kool.net is an unknown quantity at this point 
Solved Threads: 0
kool.net kool.net is offline Offline
Light Poster
 
0
  #10
Nov 3rd, 2009
hey,
how to i store value of sb in text box return from the function
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 552 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC