You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.3 KiB

  1. using System;
  2. namespace Swift.web.Library
  3. {
  4. public class NumberToWordsConverter
  5. {
  6. internal static string NumberToWord(double n)
  7. {
  8. string words = "";
  9. double intPart;
  10. double decPart = 0;
  11. if (n == 0)
  12. return "Zero";
  13. try
  14. {
  15. string[] splitter = n.ToString().Split('.');
  16. intPart = double.Parse(splitter[0]);
  17. decPart = double.Parse(splitter[1]);
  18. }
  19. catch
  20. {
  21. intPart = n;
  22. }
  23. words = NumWords(intPart);
  24. if (decPart > 0)
  25. {
  26. if (words != "")
  27. words += " and ";
  28. int counter = decPart.ToString().Length;
  29. switch (counter)
  30. {
  31. case 1: words += NumWords(decPart) + " tenths"; break;
  32. case 2: words += NumWords(decPart) + " hundredths"; break;
  33. case 3: words += NumWords(decPart) + " thousandths"; break;
  34. case 4: words += NumWords(decPart) + " ten-thousandths"; break;
  35. case 5: words += NumWords(decPart) + " hundred-thousandths"; break;
  36. case 6: words += NumWords(decPart) + " millionths"; break;
  37. case 7: words += NumWords(decPart) + " ten-millionths"; break;
  38. }
  39. }
  40. return words + " Only.";
  41. }
  42. private static String NumWords(double n) //converts double to words
  43. {
  44. string[] numbersArr = new string[] { " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine", " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen" };
  45. string[] tensArr = new string[] { " Twenty", " Thirty", " Fourty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninty" };
  46. string[] suffixesArr = new string[] { "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion", "Undecillion", "Duodecillion", "Tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septdecillion", "Octodecillion", "Novemdecillion", "Vigintillion" };
  47. string words = "";
  48. bool tens = false;
  49. if (n < 0)
  50. {
  51. words += "Negative ";
  52. n *= -1;
  53. }
  54. int power = (suffixesArr.Length + 1) * 3;
  55. while (power > 3)
  56. {
  57. double pow = Math.Pow(10, power);
  58. if (n >= pow)
  59. {
  60. if (n % pow > 0)
  61. {
  62. words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
  63. //words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
  64. }
  65. else if (n % pow == 0)
  66. {
  67. words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
  68. }
  69. n %= pow;
  70. }
  71. power -= 3;
  72. }
  73. if (n >= 1000)
  74. {
  75. if (n % 1000 > 0) words += NumWords(Math.Floor(n / 1000)) + " Thousand ";
  76. else words += NumWords(Math.Floor(n / 1000)) + " Thousand";
  77. n %= 1000;
  78. }
  79. if (0 <= n && n <= 999)
  80. {
  81. if ((int)n / 100 > 0)
  82. {
  83. words += NumWords(Math.Floor(n / 100)) + " Hundred";
  84. n %= 100;
  85. }
  86. if ((int)n / 10 > 1)
  87. {
  88. if (words != "")
  89. words += " ";
  90. words += tensArr[(int)n / 10 - 2];
  91. tens = true;
  92. n %= 10;
  93. }
  94. if (n < 20 && n > 0)
  95. {
  96. if (words != "" && tens == false)
  97. words += " ";
  98. words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
  99. n -= Math.Floor(n);
  100. }
  101. }
  102. return words;
  103. }
  104. }
  105. // end class
  106. }