GetListItemText_sv_SE.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace OpenXmlPowerTools
  8. {
  9. public class ListItemTextGetter_sv_SE
  10. {
  11. private static string[] OneThroughNineteen = {
  12. "", "ett", "två", "tre", "fyra", "fem", "sex", "sju", "åtta",
  13. "nio", "tio", "elva", "tolv", "tretton", "fjorton",
  14. "femton", "sexton", "sjutton", "arton", "nitton"
  15. };
  16. private static string[] Tens = {
  17. "","tio", "tjugo", "trettio", "fyrtio", "femtio", "sextio", "sjuttio", "åttio",
  18. "nittio", "etthundra"
  19. };
  20. private static string[] OrdinalOneThroughNineteen = {
  21. "", "första", "andra", "tredje", "fjärde", "femte", "sjätte", "sjunde",
  22. "åttonde", "nionde", "tionde", "elfte", "tolfte", "trettonde",
  23. "fjortonde", "femtonde", "sextonde", "sjuttonde",
  24. "artonde", "nittonde"
  25. };
  26. public static string GetListItemText(string languageCultureName, int levelNumber, string numFmt)
  27. {
  28. switch (numFmt)
  29. {
  30. case "cardinalText":
  31. return NumberAsCardinalText(languageCultureName, levelNumber, numFmt);
  32. case "ordinalText":
  33. return NumberAsOrdinalText(languageCultureName, levelNumber, numFmt);
  34. case "ordinal":
  35. return NumberAsOrdinal(languageCultureName, levelNumber, numFmt);
  36. default:
  37. return null;
  38. }
  39. }
  40. private static string NumberAsCardinalText(string languageCultureName, int levelNumber, string numFmt)
  41. {
  42. string result = "";
  43. var sLevel = (levelNumber + 10000).ToString();
  44. int thousands = int.Parse(sLevel.Substring(1, 1));
  45. int hundreds = int.Parse(sLevel.Substring(2, 1));
  46. int tens = int.Parse(sLevel.Substring(3, 1));
  47. int ones = int.Parse(sLevel.Substring(4, 1));
  48. //Validation
  49. if (thousands > 19)
  50. throw new ArgumentOutOfRangeException("levelNumber", "Convering a levelNumber to ordinal text that is greater then 19 999 is not supported");
  51. if (levelNumber == 0)
  52. return "Noll";
  53. if (levelNumber < 0)
  54. throw new ArgumentOutOfRangeException("levelNumber", "Converting a negative levelNumber to ordinal text is not supported");
  55. /* exact thousands */
  56. if (levelNumber == 1000)
  57. return "Ettusen";
  58. if (levelNumber > 1000 && hundreds == 0 && tens == 0 && ones == 0)
  59. {
  60. result = OneThroughNineteen[thousands] + "tusen";
  61. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  62. }
  63. /* > 1000 */
  64. if (levelNumber > 1000 && levelNumber < 2000)
  65. result = "ettusen";
  66. else if (levelNumber > 2000 && levelNumber < 10000)
  67. result = OneThroughNineteen[thousands] + "tusen";
  68. /* exact hundreds */
  69. if (hundreds > 0 && tens == 0 && ones == 0)
  70. {
  71. if (hundreds == 1)
  72. result += "etthundra";
  73. else
  74. result += OneThroughNineteen[hundreds] + "hundra";
  75. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  76. }
  77. /* > 100 */
  78. if (hundreds > 0)
  79. {
  80. if (hundreds == 1)
  81. result += "etthundra";
  82. else
  83. result += OneThroughNineteen[hundreds] + "hundra";
  84. }
  85. /* exact tens */
  86. if (tens > 0 && ones == 0)
  87. {
  88. result += Tens[tens];
  89. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  90. }
  91. /* > 20 */
  92. if (tens == 1)
  93. {
  94. result += OneThroughNineteen[tens * 10 + ones];
  95. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  96. }
  97. else if (tens > 1)
  98. {
  99. result += Tens[tens] + OneThroughNineteen[ones]; ;
  100. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  101. }
  102. else
  103. {
  104. result += OneThroughNineteen[ones];
  105. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  106. }
  107. }
  108. private static string NumberAsOrdinalText(string languageCultureName, int levelNumber, string numFmt)
  109. {
  110. string result = "";
  111. if (levelNumber <= 0)
  112. throw new ArgumentOutOfRangeException("levelNumber", "Converting a zero or negative levelNumber to ordinal text is not supported");
  113. if(levelNumber >= 10000)
  114. throw new ArgumentOutOfRangeException("levelNumber", "Convering a levelNumber to ordinal text that is greater then 10000 is not supported");
  115. if (levelNumber == 1)
  116. return "Första";
  117. var sLevel = (levelNumber + 10000).ToString();
  118. int thousands = int.Parse(sLevel.Substring(1, 1));
  119. int hundreds = int.Parse(sLevel.Substring(2, 1));
  120. int tens = int.Parse(sLevel.Substring(3, 1));
  121. int ones = int.Parse(sLevel.Substring(4, 1));
  122. /* exact thousands */
  123. if (levelNumber == 1000)
  124. return "Ettusende";
  125. if (levelNumber > 1000 && hundreds == 0 && tens == 0 && ones == 0)
  126. {
  127. result = OneThroughNineteen[thousands] + "tusende";
  128. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  129. }
  130. /* > 1000 */
  131. if (levelNumber > 1000 && levelNumber < 2000)
  132. result = "ettusen";
  133. else if (levelNumber > 2000 && levelNumber < 10000)
  134. result = OneThroughNineteen[thousands] + "tusende";
  135. /* exact hundreds */
  136. if (hundreds > 0 && tens == 0 && ones == 0)
  137. {
  138. if (hundreds == 1)
  139. result += "etthundrade";
  140. else
  141. result += OneThroughNineteen[hundreds] + "hundrade";
  142. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  143. }
  144. /* > 100 */
  145. if (hundreds > 0)
  146. {
  147. result += OneThroughNineteen[hundreds] + "hundra";
  148. }
  149. /* exact tens */
  150. if (tens > 0 && ones == 0)
  151. {
  152. result += Tens[tens] + "nde";
  153. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  154. }
  155. /* > 20 */
  156. if (tens == 1)
  157. {
  158. result += OrdinalOneThroughNineteen[tens * 10 + ones];
  159. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  160. }
  161. else if (tens > 1)
  162. {
  163. result += Tens[tens] + OrdinalOneThroughNineteen[ones]; ;
  164. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  165. }
  166. else
  167. {
  168. result += OrdinalOneThroughNineteen[ones];
  169. return result.Substring(0, 1).ToUpper() + result.Substring(1);
  170. }
  171. }
  172. private static string NumberAsOrdinal(string languageCultureName, int levelNumber, string numFmt)
  173. {
  174. string levelAsString = levelNumber.ToString();
  175. if (levelAsString == null)
  176. return "";
  177. if (levelAsString.Trim() == "")
  178. return "";
  179. if(levelAsString.EndsWith("1"))
  180. return levelAsString + ":a";
  181. else if(levelAsString.EndsWith("2"))
  182. return levelAsString + ":a";
  183. else
  184. return levelAsString + ":e";
  185. }
  186. }
  187. }