943,991 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3865
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 2nd, 2009
0

convert amount to words

Expand Post »
hi,
can any one tell me how to i convert amount(in digitts) to word.
thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
kool.net is offline Offline
28 posts
since Oct 2009
Nov 2nd, 2009
0
Re: convert amount to words
Click to Expand / Collapse  Quote originally posted by kool.net ...
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)
  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
Reputation Points: 11
Solved Threads: 12
Junior Poster
vinnijain is offline Offline
145 posts
since Jul 2009
Nov 2nd, 2009
-1

number to words

try this code

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 8
Solved Threads: 1
Newbie Poster
bk_bhupendra is offline Offline
22 posts
since Oct 2009
Nov 2nd, 2009
0
Re: convert amount to words
bk_bhupendra ,I think you must make your efforts .Copying others code and pasting is wrong..............
Reputation Points: 11
Solved Threads: 12
Junior Poster
vinnijain is offline Offline
145 posts
since Jul 2009
Nov 2nd, 2009
0
Re: convert amount to words
Although the layout could be better, this should do the trick.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Nov 2nd, 2009
0

sry

sry....
bt i thought it wil help u...
i tested it n thn i sent dis...
Reputation Points: 8
Solved Threads: 1
Newbie Poster
bk_bhupendra is offline Offline
22 posts
since Oct 2009
Nov 2nd, 2009
0
Re: convert amount to words
Click to Expand / Collapse  Quote originally posted by kool.net ...
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.............
Reputation Points: 31
Solved Threads: 36
Posting Whiz
avirag is offline Offline
312 posts
since Jun 2009
Nov 2nd, 2009
0
Re: convert amount to words
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:

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Nov 3rd, 2009
1
Re: convert amount to words
Click to Expand / Collapse  Quote originally posted by kool.net ...
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..........
Reputation Points: 31
Solved Threads: 36
Posting Whiz
avirag is offline Offline
312 posts
since Jun 2009
Nov 3rd, 2009
0
Re: convert amount to words
hey,
how to i store value of sb in text box return from the function
Reputation Points: 10
Solved Threads: 0
Light Poster
kool.net is offline Offline
28 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Web project
Next Thread in C# Forum Timeline: Deleting a datagridview record using a button click event





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC