NumToWordSp.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace FastReport.Functions
  6. {
  7. internal class NumToWordsSp : NumToWordsBase
  8. {
  9. private static Dictionary<string, CurrencyInfo> currencyList;
  10. private static WordInfo thousands = new WordInfo("mil");
  11. private static WordInfo millions = new WordInfo("millón", "millones");
  12. private static WordInfo milliards = new WordInfo("millardo", "millardos");
  13. private static WordInfo trillions = new WordInfo("billón", "billiones");
  14. //if million, milliard or trillions use un instead of uno.
  15. private bool _useUn;
  16. private static string[] hunds = {
  17. "","cien","doscientos","trescientos","cuatrocientos","quinientos","seiscientos","setecientos","ochocientos","novecientos"
  18. };
  19. private static string[] tens =
  20. {
  21. "", "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa"
  22. };
  23. private static string[] fixedWords =
  24. {
  25. "","uno","dos","tres","cuatro","cinco","seis","siete","ocho","nueve","diez","once","doce","trece","catorce","quince","dieciséis",
  26. "diecisiete","dieciocho","diecinueve"
  27. };
  28. private static string[] fixedWords21To29 =
  29. {
  30. "veintiuno","veintidós","veintitrés","veinticuatro","veinticinco","veintiséis","veintisiete","veintiocho","veintinueve"
  31. };
  32. protected override CurrencyInfo GetCurrency(string currencyName)
  33. {
  34. currencyName = currencyName.ToUpper();
  35. return currencyList[currencyName];
  36. }
  37. protected override string GetFixedWords(bool male, long value)
  38. {
  39. if (_useUn && value == 1) return "un";
  40. return fixedWords[value];
  41. }
  42. protected override string GetHund(bool male, long value)
  43. {
  44. return hunds[value / 100];
  45. }
  46. protected override WordInfo GetMilliards()
  47. {
  48. return milliards;
  49. }
  50. protected override WordInfo GetMillions()
  51. {
  52. return millions;
  53. }
  54. protected override string GetMinus()
  55. {
  56. return "menos";
  57. }
  58. protected override string GetTen(bool male, long value)
  59. {
  60. return tens[value];
  61. }
  62. protected override WordInfo GetThousands()
  63. {
  64. return thousands;
  65. }
  66. protected override WordInfo GetTrillions()
  67. {
  68. return trillions;
  69. }
  70. protected override string GetZero()
  71. {
  72. return "cero";
  73. }
  74. protected override string Str1000(long value, WordInfo info, int counter)
  75. {
  76. long val = value % 1000;
  77. if (val == 0)
  78. return "";
  79. StringBuilder r = new StringBuilder();
  80. // add hundred
  81. string hund = GetHund(info.male, val);
  82. if (hund != "")
  83. r.Append(hund);
  84. // decide whether to use the 100-10 separator or not
  85. string sep100_10 = Get100_10Separator();
  86. if (value < 1000 && hund == "")
  87. sep100_10 = "";
  88. val = val % 100;
  89. if (counter == 3 || counter == 4 || counter == 5) _useUn = true;
  90. else _useUn = false;
  91. if (val > 20 && val < 30)
  92. {
  93. r.Append(sep100_10 + fixedWords21To29[val - 21]);
  94. }
  95. else if (val < GetFixedWordsCount())
  96. {
  97. // val is less than fixed words count (usually 20), get fixed words
  98. string frac20 = GetFixedWords(info.male, val);
  99. if (frac20 != "")
  100. r.Append(sep100_10 + frac20);
  101. }
  102. else
  103. {
  104. // val is greater than fixed words count (usually 20), get tens separately
  105. string ten = GetTen(info.male, val / 10);
  106. string frac10 = GetFixedWords(info.male, val % 10);
  107. // decide whether to use 10-1 separator or not
  108. if (ten != "" && frac10 != "")
  109. r.Append(sep100_10 + ten + Get10_1Separator() + frac10);
  110. else if (ten != "")
  111. r.Append(sep100_10 + ten);
  112. else if (frac10 != "")
  113. r.Append(sep100_10 + frac10);
  114. }
  115. // add currency/group word
  116. r.Append(" ");
  117. r.Append(Case(value, info));
  118. // make the result starting with letter and ending with space
  119. if (r.Length != 0)
  120. r.Append(" ");
  121. return r.ToString().TrimStart(' ');
  122. }
  123. protected override string Get10_1Separator()
  124. {
  125. return " y ";
  126. }
  127. protected override string Get100_10Separator()
  128. {
  129. return " ";
  130. }
  131. protected override string GetDecimalSeparator()
  132. {
  133. return "y ";
  134. }
  135. static NumToWordsSp()
  136. {
  137. currencyList = new Dictionary<string, CurrencyInfo>(2);
  138. currencyList.Add("EUR", new CurrencyInfo(
  139. new WordInfo("euro", "euros"),
  140. new WordInfo("céntimo", "céntimos")));
  141. currencyList.Add("USD", new CurrencyInfo(
  142. new WordInfo("dólar", "dólares"),
  143. new WordInfo("céntimo", "céntimos")));
  144. }
  145. }
  146. }