fork download
  1. <?php
  2.  
  3.  
  4. /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
  5. function inclineWord($number, $word1, $word2, $word3) {
  6.  
  7. if ($number % 100 > 10 and $number % 100 < 20) {
  8. $number %= 100;
  9. } elseif ($number > 0) {
  10. $number %= 10;
  11. }
  12.  
  13. if ($number == 1) {
  14. $word = $word1;
  15. } elseif ($number >= 2 and $number <= 4) {
  16. $word = $word2;
  17. } elseif ($number >= 5 and $number <= 9 or $number > 10 and $number < 20 or $number == 0) {
  18. $word = $word3;
  19. }
  20.  
  21. return $word;
  22. }
  23.  
  24. /*
  25.   Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
  26.   если мы считаем число для мужского рода (один рубль),
  27.   и 1 — для женского (одна тысяча). Параметр $lengthOfNumber передает
  28.   полный размер внешнего числа и помогает обработать число ноль.
  29. */
  30. function smallNumberToText($number, $isFemale, $lengthOfNumber) {
  31.  
  32. $spelling = array(
  33. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  34. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  35. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  36. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  37. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  38. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  39. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  40. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  41. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  42. 9 => 'девять', 19 => 'девятнадцать'
  43. );
  44.  
  45. if ($isFemale) {
  46. $femaleSpelling = array(
  47. 1 => 'одна',
  48. 2 => 'две'
  49. );
  50. $spelling = array_replace($spelling, $femaleSpelling);
  51. }
  52.  
  53. $words = array();
  54.  
  55. if ($lengthOfNumber == 0 or $number >= 1 and $number <= 19) {
  56.  
  57. $words[] = $spelling[$number];
  58.  
  59. } elseif ($number >= 10 and $number <= 99) {
  60.  
  61. if ($number % 10 != 0) {
  62. $words[] = $spelling[$number % 10];
  63. }
  64. $number = floor($number / 10);
  65. $words[] = $spelling[$number * 10];
  66.  
  67. } elseif ($number >= 100 and $number <= 999) {
  68.  
  69. if ($number % 100 != 0) {
  70.  
  71. if ($number % 100 > 10 and $number % 100 < 20) {
  72.  
  73. $words[] = $spelling[$number % 100];
  74. $number = floor($number / 100);
  75.  
  76. } else {
  77.  
  78. if ($number % 10 != 0) {
  79. $words[] = $spelling[$number % 10];
  80. }
  81. $number = floor($number / 10);
  82. if ($number % 10 != 0) {
  83. $words[] = $spelling[$number % 10 * 10];
  84. }
  85. $number = floor($number / 10);
  86.  
  87. }
  88.  
  89. } else {
  90. $number = floor($number / 100);
  91. }
  92. $words[] = $spelling[$number * 100];
  93.  
  94. }
  95.  
  96. $text = implode(' ', array_reverse($words));
  97.  
  98. return $text;
  99. }
  100.  
  101. function numberToText($number) {
  102.  
  103. $divisibleNumber = $number;
  104.  
  105. $spelling = array(
  106. 0 => array(0 => 'рубль', 1 => 'рубля', 2 => 'рублей'),
  107. 1 => array(0 => 'тысяча', 1 => 'тысячи', 2 => 'тысяч'),
  108. 2 => array(0 => 'миллион', 1 => 'миллиона', 2 => 'миллионов')
  109. );
  110.  
  111. $lengthOfNumber = ($number == 0) ? 0 : floor(log10($number) + 1);
  112.  
  113. for ($x = 0; $x < ceil($lengthOfNumber / 3) or $x == 0; $x++) {
  114.  
  115. $numbers[] = $divisibleNumber % 1000;
  116. $divisibleNumber = floor($divisibleNumber / 1000);
  117.  
  118. $isFemale = ($x == 1 and $numbers[1] % 10 == 1 or $x == 1 and $numbers[1] % 10 == 2) ? 1 : 0;
  119.  
  120. $numberToText = smallNumberToText($numbers[$x], $isFemale, $lengthOfNumber);
  121. $inclinedWord = inclineWord($numbers[$x], $spelling[$x][0], $spelling[$x][1], $spelling[$x][2]);
  122.  
  123. if ($x == 0) {
  124. if ($numberToText) {
  125. $text = $numberToText;
  126. }
  127. if (isset($text)) {
  128. $text = $text.' '."($number)".' '.$inclinedWord;
  129. } else {
  130. $text = "($number)".' '.$inclinedWord;
  131. }
  132. } else {
  133. if ($numberToText) {
  134. $text = $numberToText.' '.$inclinedWord.' '.$text;
  135. }
  136. }
  137. }
  138.  
  139. return $text;
  140. }
  141.  
  142. /* Вызовем функцию несколько раз */
  143.  
  144. $amount1 = mt_rand(1,99999999);
  145. $text1 = numberToText($amount1);
  146.  
  147. echo "На вашем счету: {$text1}\n";
  148.  
  149. $amount2 = mt_rand(1,99999999);
  150. $text2 = numberToText($amount2);
  151.  
  152. echo "На вашем счету: {$text2}\n";
  153.  
  154. $amount3 = mt_rand(1,99999999);
  155. $text3 = numberToText($amount3);
  156.  
  157. echo "На вашем счету: {$text3}\n";
  158.  
  159. $amount4 = mt_rand(1,99999999);
  160. $text4 = numberToText($amount4);
  161.  
  162. echo "На вашем счету: {$text4}\n";
  163.  
  164. $amount5 = mt_rand(1,99999999);
  165. $text5 = numberToText($amount5);
  166.  
  167. echo "На вашем счету: {$text5}\n";
Success #stdin #stdout 0.02s 23704KB
stdin
Standard input is empty
stdout
На вашем счету: семьдесят два миллиона пятьсот сорок шесть тысяч пятьсот семнадцать (72546517) рублей
На вашем счету: сорок пять миллионов четыреста семьдесят одна тысяча триста сорок семь (45471347) рублей
На вашем счету: девять миллионов триста тридцать семь тысяч шестьсот сорок шесть (9337646) рублей
На вашем счету: семнадцать миллионов шестьсот сорок девять тысяч девятьсот восемьдесят три (17649983) рубля
На вашем счету: тринадцать миллионов сто пятьдесят шесть тысяч триста шестьдесят пять (13156365) рублей