NumToWordsDe.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace FastReport.Functions
  6. {
  7. internal class NumToWordsDe : NumToWordsBase
  8. {
  9. private static Dictionary<string, CurrencyInfo> currencyList;
  10. private static string[] fixedWords =
  11. {
  12. "", "ein", "zwei", "drei", "vier", "fünf", "sechs",
  13. "sieben", "acht", "neun", "zehn", "elf",
  14. "zwölf", "dreizehn", "vierzehn", "fünfzehn",
  15. "sechzehn", "siebzehn", "achtzehn", "neunzehn"
  16. };
  17. private static string[] tens =
  18. {
  19. "", "zehn", "zwanzig", "dreißig", "vierzig", "fünfzig",
  20. "sechzig", "siebzig", "achtzig", "neunzig"
  21. };
  22. private static string[] hunds =
  23. {
  24. "", "einhundert", "zweihundert", "dreihundert", "vierhundert",
  25. "fünfhundert", "sechshundert", "siebenhundert", "achthundert", "neunhundert"
  26. };
  27. private static WordInfo thousands = new WordInfo(false, "tausend", "tausend", "tausend");
  28. private static WordInfo millions = new WordInfo(false, "Million", "Millionen", "Millionen");
  29. private static WordInfo milliards = new WordInfo(false, "Milliarde", "Milliarden", "Milliarden");
  30. private static WordInfo trillions = new WordInfo(false, "Billion", "Billionen", "Billionen");
  31. protected override string Str1000(long value, WordInfo info, int counter)
  32. {
  33. long val = value % 1000;
  34. if (val == 0)
  35. return "";
  36. StringBuilder r = new StringBuilder();
  37. // add hundred
  38. string hund = GetHund(info.male, val);
  39. if (hund != "")
  40. r.Append(hund);
  41. val = val % 100;
  42. if (val < GetFixedWordsCount())
  43. {
  44. // val is less than fixed words count (usually 20), get fixed words
  45. string frac20 = GetFixedWords(info.male, val);
  46. if (frac20 != "")
  47. r.Append(frac20);
  48. }
  49. else
  50. {
  51. // val is greater than fixed words count (usually 20), get tens separately
  52. string ten = GetTen(info.male, val / 10);
  53. string frac10 = GetFixedWords(info.male, val % 10);
  54. // decide whether to use 10-1 separator or not
  55. if (ten != "" && frac10 != "")
  56. r.Append(frac10 + "und" + ten);
  57. else if (ten != "")
  58. r.Append(ten);
  59. else if (frac10 != "")
  60. r.Append(frac10);
  61. }
  62. string separator = counter == 2 ? "" : " "; // thousands do not use separator
  63. // add currency/group word
  64. r.Append(separator);
  65. r.Append(Case(value, info));
  66. // make the result starting with letter and ending with space
  67. if (r.Length != 0)
  68. r.Append(separator);
  69. return r.ToString().TrimStart(' ');
  70. }
  71. protected override string GetFixedWords(bool male, long value)
  72. {
  73. string result = fixedWords[value];
  74. if (!male)
  75. {
  76. if (value == 1)
  77. return "eine";
  78. }
  79. return result;
  80. }
  81. protected override string GetTen(bool male, long value)
  82. {
  83. return tens[value];
  84. }
  85. protected override string GetHund(bool male, long value)
  86. {
  87. return hunds[value / 100];
  88. }
  89. protected override WordInfo GetThousands()
  90. {
  91. return thousands;
  92. }
  93. protected override WordInfo GetMillions()
  94. {
  95. return millions;
  96. }
  97. protected override WordInfo GetMilliards()
  98. {
  99. return milliards;
  100. }
  101. protected override WordInfo GetTrillions()
  102. {
  103. return trillions;
  104. }
  105. protected override CurrencyInfo GetCurrency(string currencyName)
  106. {
  107. currencyName = currencyName.ToUpper();
  108. if (currencyName == "CAD")
  109. currencyName = "USD";
  110. return currencyList[currencyName];
  111. }
  112. protected override string GetZero()
  113. {
  114. return "null";
  115. }
  116. protected override string GetMinus()
  117. {
  118. return "minus";
  119. }
  120. protected override string GetDecimalSeparator()
  121. {
  122. return "und ";
  123. }
  124. static NumToWordsDe()
  125. {
  126. currencyList = new Dictionary<string, CurrencyInfo>();
  127. currencyList.Add("USD", new CurrencyInfo(
  128. new WordInfo("Dollar", "Dollar"),
  129. new WordInfo("Cent", "Cent")));
  130. currencyList.Add("EUR", new CurrencyInfo(
  131. new WordInfo("Euro", "Euro"),
  132. new WordInfo("Cent", "Cent")));
  133. currencyList.Add("GBP", new CurrencyInfo(
  134. new WordInfo("Pfund", "Pfund"),
  135. new WordInfo("Penny", "Penny")));
  136. }
  137. }
  138. }