NumToWordsFr.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace FastReport.Functions
  6. {
  7. internal class NumToWordsFr : NumToWordsBase
  8. {
  9. private static Dictionary<string, CurrencyInfo> currencyList;
  10. private static string[] fixedWords =
  11. {
  12. "", "un", "deux", "trois", "quatre", "cinq", "six",
  13. "sept", "huit", "neuf", "dix", "onze",
  14. "douze", "treize", "quatorze", "quinze",
  15. "seize", "dix-sept", "dix-huit", "dix-neuf"
  16. };
  17. private static string[] fixedWords71to79 =
  18. {
  19. "soixante et onze", "soixante-douze", "soixante-treize", "soixante-quatorze",
  20. "soixante-quinze", "soixante-seize", "soixante-dix-sept", "soixante-dix-huit", "soixante-dix-neuf"
  21. };
  22. private static string[] fixedWords91to99 =
  23. {
  24. "quatre-vingt-onze", "quatre-vingt-douze", "quatre-vingt-treize", "quatre-vingt-quatorze",
  25. "quatre-vingt-quinze", "quatre-vingt-seize", "quatre-vingt-dix-sept", "quatre-vingt-dix-huit", "quatre-vingt-dix-neuf"
  26. };
  27. private static string[] tens =
  28. {
  29. "", "dix", "vingt", "trente", "quarante", "cinquante",
  30. "soixante", "soixante-dix", "quatre-vingt", "quatre-vingt-dix"
  31. };
  32. private static string[] hunds =
  33. {
  34. "", "cent", "deux cent", "trois cent", "quatre cent",
  35. "cinq cent", "six cent", "sept cent", "huit cent", "neuf cent"
  36. };
  37. private static WordInfo thousands = new WordInfo(false, "mille", "mille", "mille");
  38. private static WordInfo millions = new WordInfo(false, "million", "millions", "millions");
  39. private static WordInfo milliards = new WordInfo(false, "milliard", "milliards", "milliards");
  40. private static WordInfo trillions = new WordInfo(false, "billion", "billions", "billions");
  41. protected override string Str1000(long value, WordInfo info, int counter)
  42. {
  43. long val = value % 1000;
  44. if (val == 0)
  45. return "";
  46. StringBuilder r = new StringBuilder();
  47. // add hundred
  48. string hund = GetHund(info.male, val);
  49. if (hund != "")
  50. r.Append(hund);
  51. // decide whether to use the 100-10 separator or not
  52. string sep100_10 = Get100_10Separator();
  53. if (value < 1000 && hund == "")
  54. sep100_10 = "";
  55. val = val % 100;
  56. if (val < 20)
  57. {
  58. r.Append(sep100_10 + GetFixedWords(info.male, val));
  59. }
  60. else if (val > 70 && val < 80)
  61. {
  62. r.Append(sep100_10 + fixedWords71to79[val - 71]);
  63. }
  64. else if (val > 90 && val < 100)
  65. {
  66. r.Append(sep100_10 + fixedWords91to99[val - 91]);
  67. }
  68. else
  69. {
  70. // val is greater than fixed words count (usually 20), get tens separately
  71. string ten = GetTen(info.male, val / 10);
  72. string frac10 = GetFixedWords(info.male, val % 10);
  73. // decide whether to use 10-1 separator or not
  74. if (ten != "" && frac10 != "")
  75. {
  76. string sep10_1 = "-";
  77. if (val % 10 == 1)
  78. if (val / 10 != 8)
  79. sep10_1 = " et ";
  80. r.Append(sep100_10 + ten + sep10_1 + frac10);
  81. }
  82. else if (ten != "")
  83. r.Append(sep100_10 + ten);
  84. else if (frac10 != "")
  85. r.Append(sep100_10 + frac10);
  86. }
  87. // special cases
  88. if (counter == 1) // the final
  89. {
  90. // 80 ends with 's' in case it's the final word
  91. if (val == 80)
  92. r.Append("s");
  93. // 100's ends with 's' in case it's the final word
  94. if (val == 0 && value % 1000 > 100)
  95. r.Append("s");
  96. }
  97. // add currency/group word
  98. r.Append(" ");
  99. r.Append(Case(value, info));
  100. // make the result starting with letter and ending with space
  101. if (r.Length != 0)
  102. r.Append(" ");
  103. return r.ToString().TrimStart(' ');
  104. }
  105. protected override string GetFixedWords(bool male, long value)
  106. {
  107. string result = fixedWords[value];
  108. if (!male)
  109. {
  110. if (value == 1)
  111. return "une";
  112. }
  113. return result;
  114. }
  115. protected override string GetTen(bool male, long value)
  116. {
  117. return tens[value];
  118. }
  119. protected override string GetHund(bool male, long value)
  120. {
  121. return hunds[value / 100];
  122. }
  123. protected override WordInfo GetThousands()
  124. {
  125. return thousands;
  126. }
  127. protected override WordInfo GetMillions()
  128. {
  129. return millions;
  130. }
  131. protected override WordInfo GetMilliards()
  132. {
  133. return milliards;
  134. }
  135. protected override WordInfo GetTrillions()
  136. {
  137. return trillions;
  138. }
  139. protected override CurrencyInfo GetCurrency(string currencyName)
  140. {
  141. currencyName = currencyName.ToUpper();
  142. if (currencyName == "CAD")
  143. currencyName = "USD";
  144. return currencyList[currencyName];
  145. }
  146. protected override string GetZero()
  147. {
  148. return "zéro";
  149. }
  150. protected override string GetMinus()
  151. {
  152. return "moins";
  153. }
  154. protected override string GetDecimalSeparator()
  155. {
  156. return "et ";
  157. }
  158. static NumToWordsFr()
  159. {
  160. currencyList = new Dictionary<string, CurrencyInfo>();
  161. currencyList.Add("USD", new CurrencyInfo(
  162. new WordInfo("dollar", "dollars"),
  163. new WordInfo("cent", "cents")));
  164. currencyList.Add("EUR", new CurrencyInfo(
  165. new WordInfo("euro", "euros"),
  166. new WordInfo("cent", "cents")));
  167. currencyList.Add("GBP", new CurrencyInfo(
  168. new WordInfo("GBP", "GBP"),
  169. new WordInfo("penny", "penny")));
  170. }
  171. }
  172. }