NumToWordsEs.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace FastReport.Functions
  6. {
  7. internal class NumToWordsEs : NumToWordsBase
  8. {
  9. private static Dictionary<string, CurrencyInfo> currencyList;
  10. private static string[] fixedWords =
  11. {
  12. "", "un", "dos", "tres", "cuatro", "cinco", "seis",
  13. "siete", "ocho", "nueve", "diez", "once",
  14. "doce", "trece", "catorce", "quince",
  15. "dieciséis", "diecisiete", "dieciocho", "diecinueve",
  16. "veinte", "veintiún", "veintidós", "veintitrés", "veinticuatro",
  17. "veinticinco", "veintiséis", "veintisiete", "veintiocho", "veintinueve"
  18. };
  19. private static string[] tens =
  20. {
  21. "", "diez", "veinte", "treinta", "cuarenta", "cincuenta",
  22. "sesenta", "setenta", "ochenta", "noventa"
  23. };
  24. private static string[] hunds =
  25. {
  26. "", "cien", "doscientos", "trescientos", "cuatrocientos",
  27. "quinientos", "seiscientos", "setecientos", "ochocientos", "novecientos"
  28. };
  29. private static WordInfo thousands = new WordInfo("mil");
  30. private static WordInfo millions = new WordInfo("millón", "millones");
  31. private static WordInfo milliards = new WordInfo("mil");
  32. private static WordInfo trillions = new WordInfo("billón", "billones");
  33. protected override void Str(long value, WordInfo senior, StringBuilder result)
  34. {
  35. if (value == 0)
  36. {
  37. result.Append(GetZero() + " " + senior.many + " ");
  38. }
  39. else if (value == 1)
  40. {
  41. if (senior.male)
  42. result.Append("un ");
  43. else
  44. result.Append("una ");
  45. result.Append(senior.one).Append(" ");
  46. }
  47. else
  48. {
  49. if (value % 1000 != 0)
  50. result.Append(Str1000(value, senior, 1).Replace("veintiún", "veintiun"));
  51. else
  52. result.Append(senior.many + " ");
  53. value /= 1000;
  54. result.Insert(0, Str1000(value, GetThousands(), 2));
  55. value /= 1000;
  56. result.Insert(0, Str1000(value, GetMillions(), 3));
  57. // in spanish, the "milliard" is not used. They use "thousand of million" instead
  58. bool hasMillions = value % 1000 > 0;
  59. value /= 1000;
  60. string thousandsOfMillions = Str1000(value, GetThousands(), 4);
  61. if (value > 0 && !hasMillions)
  62. thousandsOfMillions += "millones ";
  63. result.Insert(0, thousandsOfMillions);
  64. value /= 1000;
  65. result.Insert(0, Str1000(value, GetTrillions(), 5));
  66. }
  67. }
  68. protected override string Case(long value, WordInfo info)
  69. {
  70. // return things (dollars, euros, pages, etc) in the plural form unless it is 1.
  71. // the "1" case is handled in the Str method.
  72. if (info == thousands || info == millions || info == milliards || info == trillions)
  73. {
  74. if (value == 1)
  75. return info.one;
  76. }
  77. return info.many;
  78. }
  79. protected override int GetFixedWordsCount()
  80. {
  81. return 30;
  82. }
  83. protected override string GetFixedWords(bool male, long value)
  84. {
  85. return fixedWords[value];
  86. }
  87. protected override string GetTen(bool male, long value)
  88. {
  89. return tens[value];
  90. }
  91. protected override string GetHund(bool male, long value)
  92. {
  93. if (value / 100 == 1 && value % 100 != 0)
  94. return "ciento";
  95. return hunds[value / 100];
  96. }
  97. protected override WordInfo GetThousands()
  98. {
  99. return thousands;
  100. }
  101. protected override WordInfo GetMillions()
  102. {
  103. return millions;
  104. }
  105. protected override WordInfo GetMilliards()
  106. {
  107. return milliards;
  108. }
  109. protected override WordInfo GetTrillions()
  110. {
  111. return trillions;
  112. }
  113. protected override CurrencyInfo GetCurrency(string currencyName)
  114. {
  115. currencyName = currencyName.ToUpper();
  116. if (currencyName == "CAD")
  117. currencyName = "USD";
  118. return currencyList[currencyName];
  119. }
  120. protected override string GetZero()
  121. {
  122. return "cero";
  123. }
  124. protected override string GetMinus()
  125. {
  126. return "minus";
  127. }
  128. protected override string GetDecimalSeparator()
  129. {
  130. return "con ";
  131. }
  132. protected override string Get10_1Separator()
  133. {
  134. return " y ";
  135. }
  136. static NumToWordsEs()
  137. {
  138. currencyList = new Dictionary<string, CurrencyInfo>();
  139. currencyList.Add("USD", new CurrencyInfo(
  140. new WordInfo("dolar", "dolares"),
  141. new WordInfo("centavo", "centavos")));
  142. currencyList.Add("EUR", new CurrencyInfo(
  143. new WordInfo("euro", "euros"),
  144. new WordInfo("centavo", "centavos")));
  145. currencyList.Add("MXN", new CurrencyInfo(
  146. new WordInfo("peso", "pesos"),
  147. new WordInfo("centavo", "centavos")));
  148. }
  149. }
  150. }