NumberToWords.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. namespace UICatalog;
  3. public static class NumberToWords
  4. {
  5. private static readonly string [] tens =
  6. {
  7. "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
  8. };
  9. private static readonly string [] units =
  10. {
  11. "Zero",
  12. "One",
  13. "Two",
  14. "Three",
  15. "Four",
  16. "Five",
  17. "Six",
  18. "Seven",
  19. "Eight",
  20. "Nine",
  21. "Ten",
  22. "Eleven",
  23. "Twelve",
  24. "Thirteen",
  25. "Fourteen",
  26. "Fifteen",
  27. "Sixteen",
  28. "Seventeen",
  29. "Eighteen",
  30. "Nineteen"
  31. };
  32. public static string Convert (long i)
  33. {
  34. if (i < 20)
  35. {
  36. return units [i];
  37. }
  38. if (i < 100)
  39. {
  40. return tens [i / 10] + (i % 10 > 0 ? " " + Convert (i % 10) : "");
  41. }
  42. if (i < 1000)
  43. {
  44. return units [i / 100]
  45. + " Hundred"
  46. + (i % 100 > 0 ? " And " + Convert (i % 100) : "");
  47. }
  48. if (i < 100000)
  49. {
  50. return Convert (i / 1000)
  51. + " Thousand "
  52. + (i % 1000 > 0 ? " " + Convert (i % 1000) : "");
  53. }
  54. if (i < 10000000)
  55. {
  56. return Convert (i / 100000)
  57. + " Lakh "
  58. + (i % 100000 > 0 ? " " + Convert (i % 100000) : "");
  59. }
  60. if (i < 1000000000)
  61. {
  62. return Convert (i / 10000000)
  63. + " Crore "
  64. + (i % 10000000 > 0 ? " " + Convert (i % 10000000) : "");
  65. }
  66. return Convert (i / 1000000000)
  67. + " Arab "
  68. + (i % 1000000000 > 0 ? " " + Convert (i % 1000000000) : "");
  69. }
  70. public static string ConvertAmount (double amount)
  71. {
  72. try
  73. {
  74. var amount_int = (long)amount;
  75. var amount_dec = (long)Math.Round ((amount - amount_int) * 100);
  76. if (amount_dec == 0)
  77. {
  78. return Convert (amount_int) + " Only.";
  79. }
  80. return Convert (amount_int) + " Point " + Convert (amount_dec) + " Only.";
  81. }
  82. catch (Exception e)
  83. {
  84. throw new ArgumentOutOfRangeException (e.Message);
  85. }
  86. }
  87. }