NumToWordsIn.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Text;
  2. using System.Collections.Generic;
  3. namespace FastReport.Functions
  4. {
  5. internal class NumToWordsIn : NumToWordsBase
  6. {
  7. private bool shouldReturnMany = false; //if third or fourth counter, so use lakh/crore instead of thousands/millions
  8. private static Dictionary<string, CurrencyInfo> currencyList;
  9. private static string[] fixedWords =
  10. {
  11. "", "one", "two", "three", "four", "five", "six", "seven",
  12. "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
  13. "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
  14. };
  15. private static string[] tens =
  16. {
  17. "", "ten", "twenty", "thirty",
  18. "forty", "fifty", "sixty", "seventy",
  19. "eighty", "ninety"
  20. };
  21. private static string[] hunds =
  22. {
  23. "", "one hundred", "two hundred", "three hundred", "four hundred",
  24. "five hundred", "six hundred", "seven hundred", "eight hundred", "nine hundred"
  25. };
  26. private static WordInfo thousands = new WordInfo("thousand", "crore");
  27. private static WordInfo millions = new WordInfo("million", "lakh");
  28. private static WordInfo milliards = new WordInfo("billion", "crore");
  29. private static WordInfo trillions = new WordInfo("trillion");
  30. protected override CurrencyInfo GetCurrency(string currencyName)
  31. {
  32. currencyName = currencyName.ToUpper();
  33. return currencyList[currencyName];
  34. }
  35. protected override string GetFixedWords(bool male, long value)
  36. {
  37. return fixedWords[value];
  38. }
  39. protected override string GetHund(bool male, long value)
  40. {
  41. return hunds[value / 100];
  42. }
  43. protected override WordInfo GetMilliards()
  44. {
  45. return milliards;
  46. }
  47. protected override WordInfo GetMillions()
  48. {
  49. return millions;
  50. }
  51. protected override string GetMinus()
  52. {
  53. return "minus";
  54. }
  55. protected override string GetTen(bool male, long value)
  56. {
  57. return tens[value];
  58. }
  59. protected override WordInfo GetThousands()
  60. {
  61. return thousands;
  62. }
  63. protected override WordInfo GetTrillions()
  64. {
  65. return trillions;
  66. }
  67. protected override string GetZero()
  68. {
  69. return "zero";
  70. }
  71. protected override string GetDecimalSeparator()
  72. {
  73. return "and ";
  74. }
  75. protected override string Get10_1Separator()
  76. {
  77. return "-";
  78. }
  79. protected override void Str(long value, WordInfo senior, StringBuilder result)
  80. {
  81. if (value == 0)
  82. result.Append(GetZero() + " " + senior.many + " ");
  83. else
  84. {
  85. if (value % 1000 != 0)
  86. result.Append(Str1000(value, senior, 1));
  87. else
  88. result.Append(" " + senior.many + " ");
  89. value /= 1000;
  90. //grouping digits not by threes as in international system, but by sets of two digits
  91. result.Insert(0, Str1000(value, GetThousands(), 2));
  92. value /= 100;
  93. result.Insert(0, Str1000(value, GetMillions(), 3));
  94. value /= 100;
  95. result.Insert(0, Str1000(value, GetMilliards(), 4));
  96. value /= 100;
  97. result.Insert(0, Str1000(value, new WordInfo("arab"), 5));
  98. value /= 100;
  99. result.Insert(0, Str1000(value, new WordInfo("kharab"), 6));
  100. value /= 100;
  101. result.Insert(0, Str1000(value, new WordInfo("nil"), 7));
  102. result.Replace(" ", " ");
  103. }
  104. }
  105. protected override string Case(long value, WordInfo info)
  106. {
  107. if (shouldReturnMany == true)
  108. {
  109. return info.many;
  110. }
  111. return info.one;
  112. /*if (value % 100 == 1)
  113. return info.one;
  114. return info.many;*/
  115. }
  116. protected override string Str1000(long value, WordInfo info, int counter)
  117. {
  118. long val;
  119. //if its third or
  120. if (counter == 3 || counter == 4) shouldReturnMany = true;
  121. else shouldReturnMany = false;
  122. if (counter == 1) val = value % 1000;
  123. else val = value % 100;
  124. if (val == 0)
  125. return "";
  126. StringBuilder r = new StringBuilder();
  127. // add hundred
  128. string hund = GetHund(info.male, val);
  129. if (hund != "")
  130. r.Append(hund);
  131. // decide whether to use the 100-10 separator or not
  132. string sep100_10 = Get100_10Separator();
  133. if (value < 1000 && hund == "")
  134. sep100_10 = "";
  135. val = val % 100;
  136. if (val < GetFixedWordsCount())
  137. {
  138. // val is less than fixed words count (usually 20), get fixed words
  139. string frac20 = GetFixedWords(info.male, val);
  140. if (frac20 != "")
  141. r.Append(sep100_10 + frac20);
  142. }
  143. else
  144. {
  145. // val is greater than fixed words count (usually 20), get tens separately
  146. string ten = GetTen(info.male, val / 10);
  147. string frac10 = GetFixedWords(info.male, val % 10);
  148. // decide whether to use 10-1 separator or not
  149. if (ten != "" && frac10 != "")
  150. r.Append(sep100_10 + ten + Get10_1Separator() + frac10);
  151. else if (ten != "")
  152. r.Append(sep100_10 + ten);
  153. else if (frac10 != "")
  154. r.Append(sep100_10 + frac10);
  155. }
  156. //Twenty-Four crore and seventy-Five lakh and Eighty-Zero thousand rupees and 00 paise
  157. // add currency/group word
  158. r.Append(" ");
  159. if (counter == 1 && value % 100 != 1)
  160. {
  161. shouldReturnMany = true;
  162. }
  163. r.Append(Case(value, info));
  164. // make the result starting with letter and ending with space
  165. if (r.Length != 0)
  166. r.Append(" ");
  167. return r.ToString().TrimStart(' ');
  168. }
  169. static NumToWordsIn()
  170. {
  171. currencyList = new Dictionary<string, CurrencyInfo>();
  172. currencyList.Add("USD", new CurrencyInfo(
  173. new WordInfo("dollar", "dollars"),
  174. new WordInfo("cent", "cents")));
  175. currencyList.Add("EUR", new CurrencyInfo(
  176. new WordInfo("euro", "euros"),
  177. new WordInfo("cent", "cents")));
  178. currencyList.Add("INR", new CurrencyInfo(
  179. new WordInfo("rupee", "rupees"),
  180. new WordInfo("paise")
  181. ));
  182. }
  183. }
  184. }