Задача: Преобразование сумм из цифрового представления в строковое
Исходник: числительные - прописью, язык: C# [code #582, hits: 50658]
аноним: student [добавлен: 25.01.2009]
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Rsdn.Janus.Framework
  5. {
  6. /// <summary>
  7. /// A set of C# methods for spelling Russian numerics
  8. /// Copyright (c) 2002-2008 Rsdn Group
  9. /// </summary>
  10. public static class RusNumber
  11. {
  12. private static readonly string[] _frac20Male =
  13. new[]
  14. {
  15. "", "один", "два", "три", "четыре", "пять", "шесть",
  16. "семь", "восемь", "девять", "десять", "одиннадцать",
  17. "двенадцать", "тринадцать", "четырнадцать", "пятнадцать",
  18. "шестнадцать", "семнадцать", "восемнадцать", "девятнадцать"
  19. };
  20.  
  21. private static readonly string[] _frac20Female =
  22. new[]
  23. {
  24. "", "одна", "две", "три", "четыре", "пять", "шесть",
  25. "семь", "восемь", "девять", "десять", "одиннадцать",
  26. "двенадцать", "тринадцать", "четырнадцать", "пятнадцать",
  27. "шестнадцать", "семнадцать", "восемнадцать", "девятнадцать"
  28. };
  29.  
  30. private static readonly string[] _hunds =
  31. {
  32. "", "сто", "двести", "триста", "четыреста",
  33. "пятьсот", "шестьсот", "семьсот", "восемьсот", "девятьсот"
  34. };
  35.  
  36. private static readonly string[] _tens =
  37. {
  38. "", "десять", "двадцать", "тридцать", "сорок", "пятьдесят",
  39. "шестьдесят", "семьдесят", "восемьдесят", "девяносто"
  40. };
  41.  
  42. public static string RusSpelledOut(
  43. this decimal value,
  44. bool male)
  45. {
  46. if (value >= 1000000000000000)
  47. throw new ArgumentOutOfRangeException("value");
  48.  
  49. var str = new StringBuilder();
  50.  
  51. if (value < 0)
  52. {
  53. str.Append("минус");
  54. value = -value;
  55. }
  56.  
  57. value = value
  58. .AppendPeriod(1000000000000, str, "триллион", "триллиона", "триллионов", true)
  59. .AppendPeriod(1000000000, str, "миллиард", "миллиарда", "миллиардов", true)
  60. .AppendPeriod(1000000, str, "миллион", "миллиона", "миллионов", true)
  61. .AppendPeriod(1000, str, "тысяча", "тысячи", "тысяч", false);
  62.  
  63. var hundreds = (int)(value / 100);
  64. if (hundreds != 0)
  65. str.AppendWithSpace(_hunds[hundreds]);
  66.  
  67. var less100 = (int)(value % 100);
  68. var frac20 = male ? _frac20Male : _frac20Female;
  69. if (less100 < 20)
  70. str.AppendWithSpace(frac20[less100]);
  71. else
  72. {
  73. var tens = less100 / 10;
  74. str.AppendWithSpace(_tens[tens]);
  75. var less10 = less100 % 10;
  76. if (less10 != 0)
  77. str.Append(" " + frac20[less100%10]);
  78. }
  79.  
  80. return str.ToString();
  81. }
  82.  
  83. private static void AppendWithSpace(this StringBuilder stringBuilder, string str)
  84. {
  85. if (stringBuilder.Length > 0)
  86. stringBuilder.Append(" ");
  87. stringBuilder.Append(str);
  88. }
  89.  
  90. private static decimal AppendPeriod(
  91. this decimal value,
  92. long power,
  93. StringBuilder str,
  94. string declension1,
  95. string declension2,
  96. string declension5,
  97. bool male)
  98. {
  99. var thousands =(int)(value / power);
  100. if (thousands > 0)
  101. {
  102. str.AppendWithSpace(
  103. ((decimal)thousands).RusSpelledOut(male, declension1, declension2, declension5));
  104. return value%power;
  105. }
  106. return value;
  107. }
  108.  
  109. public static string RusSpelledOut(
  110. this decimal value,
  111. bool male,
  112. string valueDeclensionFor1,
  113. string valueDeclensionFor2,
  114. string valueDeclensionFor5)
  115. {
  116. return
  117. RusSpelledOut(value, male)
  118. + " "
  119. + ((int)(value % 10)).GetDeclension(valueDeclensionFor1, valueDeclensionFor2, valueDeclensionFor5);
  120. }
  121. }
  122. }
  123.  
  124.  
  125. public static string GetDeclension(this int val, string one, string two, string five) {
  126. var t = (val % 100 > 20) ? val % 10 : val % 20;
  127.  
  128. switch (t)
  129. {
  130. case 1:
  131. return one;
  132. case 2:
  133. case 3:
  134. case 4:
  135. return two;
  136. default:
  137. return five;
  138. }
  139. }
преобразование числительных - в русскую строку прописью

+добавить реализацию