NumberToWords.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace UICatalog {
  7. public static class NumberToWords {
  8. private static String [] units = { "Zero", "One", "Two", "Three",
  9. "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
  10. "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
  11. "Seventeen", "Eighteen", "Nineteen" };
  12. private static String [] tens = { "", "", "Twenty", "Thirty", "Forty",
  13. "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
  14. public static String ConvertAmount (double amount)
  15. {
  16. try {
  17. Int64 amount_int = (Int64)amount;
  18. Int64 amount_dec = (Int64)Math.Round ((amount - (double)(amount_int)) * 100);
  19. if (amount_dec == 0) {
  20. return Convert (amount_int) + " Only.";
  21. } else {
  22. return Convert (amount_int) + " Point " + Convert (amount_dec) + " Only.";
  23. }
  24. } catch (Exception e) {
  25. throw new ArgumentOutOfRangeException (e.Message);
  26. }
  27. }
  28. public static String Convert (Int64 i)
  29. {
  30. if (i < 20) {
  31. return units [i];
  32. }
  33. if (i < 100) {
  34. return tens [i / 10] + ((i % 10 > 0) ? " " + Convert (i % 10) : "");
  35. }
  36. if (i < 1000) {
  37. return units [i / 100] + " Hundred"
  38. + ((i % 100 > 0) ? " And " + Convert (i % 100) : "");
  39. }
  40. if (i < 100000) {
  41. return Convert (i / 1000) + " Thousand "
  42. + ((i % 1000 > 0) ? " " + Convert (i % 1000) : "");
  43. }
  44. if (i < 10000000) {
  45. return Convert (i / 100000) + " Lakh "
  46. + ((i % 100000 > 0) ? " " + Convert (i % 100000) : "");
  47. }
  48. if (i < 1000000000) {
  49. return Convert (i / 10000000) + " Crore "
  50. + ((i % 10000000 > 0) ? " " + Convert (i % 10000000) : "");
  51. }
  52. return Convert (i / 1000000000) + " Arab "
  53. + ((i % 1000000000 > 0) ? " " + Convert (i % 1000000000) : "");
  54. }
  55. }
  56. }