fork(2) download
  1. <?php
  2. // Staring straight up into the sky ... oh my my
  3.  
  4. /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
  5. function inclineWord($number, $word1, $word2, $word5)
  6. {
  7. $last2Digits = $number % 100;
  8. if ($last2Digits > 19) { $last2Digits = $last2Digits % 10; }
  9. switch ($last2Digits) {
  10. case 1: { return " {$word1}"; }
  11. case 2: case 3: case 4: { return " {$word2}"; }
  12. default: { return " {$word5}"; }
  13. }
  14. }
  15.  
  16. /*
  17. Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
  18. если мы считаем число для мужского рода (один рубль),
  19. и 1 — для женского (одна тысяча)
  20. */
  21. function smallNumberToText($number, $isFemale)
  22. {
  23.  
  24. $spelling = array(
  25. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  26. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  27. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  28. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  29. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  30. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  31. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  32. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  33. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  34. 9 => 'девять', 19 => 'девятнадцать'
  35. );
  36.  
  37. $femaleSpelling = array(
  38. 1 => 'одна',
  39. 2 => 'две'
  40. );
  41.  
  42. $str = array();
  43.  
  44. if($number == 0){
  45. return null;
  46. }
  47.  
  48. if (array_key_exists($number, $spelling)) {
  49. return $spelling[$number];
  50. }
  51.  
  52. $numberLast2Digits = $number % 100;
  53.  
  54. $hundreds = floor($number / 100);
  55. $tens = floor($number % 100 / 10) * 10;
  56. $units = $number % 10;
  57.  
  58. if ($hundreds > 0) {
  59. echo $hundreds;
  60. array_push($str, $spelling[$hundreds]);
  61. }
  62. if($last2Digits >= 11 && $last2Digits <= 19){
  63. array_push($str, $spelling[$numberLast2Digits]);
  64. }
  65. else{
  66. if ($tens > 0) {
  67. array_push($str, $spelling[$tens]);
  68. }
  69. if ($units > 0) {
  70. array_push($str, $spelling[$units]);
  71. }
  72. }
  73. $str = implode($str, " ");
  74. return trim($str);
  75. }
  76.  
  77. function numberToText($number)
  78. {
  79.  
  80. if($number == 0){
  81. $str = "ноль";
  82. $str .= inclineWord($number, "рубль", "рубля", "рублей");
  83. return $str;
  84. }
  85.  
  86. $hundredsDigits = $number % 1000;
  87. $thousandsDigits = (floor($number / 1000)) % 1000;
  88. $millionsDigits = ((floor($number / 1000)) / 1000) % 1000;
  89.  
  90. $hundredsWords = smallNumberToText($hundredsDigits, 0);
  91.  
  92. if($thousandsDigits == 1 || $thousandsDigits == 2){
  93. $thousandsWords = smallNumberToText($thousandsDigits, 1);
  94. }
  95. else{
  96. $thousandsWords = smallNumberToText($thousandsDigits, 0);
  97. }
  98.  
  99. $millionsWords = smallNumberToText($millionsDigits, 0);
  100.  
  101. if($millionsDigits > 0){
  102. $millionsWords .= inclineWord($millionsDigits, "миллион", "миллиона", "миллионов");
  103. }
  104. if($thousandsDigits > 0){
  105. $thousandsWords .= inclineWord($thousandsDigits, "тысяча", "тысячи", "тысяч");
  106. }
  107.  
  108. echo "cотни: {$hundredsDigits}";
  109.  
  110. $words = array($millionsWords, $thousandsWords, $hundredsWords);
  111. $words = array_filter($words);
  112.  
  113. $str = implode($words, " ");
  114. $str .= inclineWord($number, "рубль", "рубля", "рублей");
  115. return trim($str);
  116. }
  117.  
  118. /* Вызовем функцию несколько раз */
  119. $amount1 = 999999999;
  120. $text1 = numberToText($amount1);
  121.  
  122. echo "На вашем счету {$text1}\n";
  123.  
  124. $amount2 = 0;
  125. $text2 = numberToText($amount2);
  126.  
  127. echo "На вашем счету {$text2}\n";
  128.  
  129. $amount3 = 1;
  130. $text3 = numberToText($amount3);
  131.  
  132. echo "На вашем счету {$text3}\n";
  133.  
  134. $amount4 = 11012013;
  135. $text4 = numberToText($amount4);
  136.  
  137. echo "На вашем счету {$text4}\n";
  138.  
  139. $amount5 = 7000008;
  140. $text5 = numberToText($amount5);
  141.  
  142. echo "На вашем счету {$text5}\n";
  143.  
  144. $amount6 = 1002;
  145. $text6 = numberToText($amount6);
  146.  
  147. echo "На вашем счету {$text6}\n";
  148.  
  149. $amount7 = 1000;
  150. $text7 = numberToText($amount7);
  151.  
  152. echo "На вашем счету {$text7}\n";
  153.  
  154. $amount8 = 7000000;
  155. $text8 = numberToText($amount8);
  156.  
  157. echo "На вашем счету {$text8}\n";
Success #stdin #stdout #stderr 0.02s 24348KB
stdin
Standard input is empty
stdout
999cотни: 999На вашем счету девять девяносто девять миллионов девять девяносто девять тысяч девять девяносто девять рублей
На вашем счету ноль рублей
cотни: 1На вашем счету один рубль
cотни: 13На вашем счету одиннадцать миллионов двенадцать тысяч тринадцать рублей
cотни: 8На вашем счету семь миллионов восемь рублей
cотни: 2На вашем счету один тысяча два рубля
cотни: 0На вашем счету один тысяча рублей
cотни: 0На вашем счету семь миллионов рублей
stderr
PHP Notice:  Undefined variable: last2Digits in /home/3KstP7/prog.php on line 64
PHP Notice:  Undefined variable: last2Digits in /home/3KstP7/prog.php on line 64
PHP Notice:  Undefined variable: last2Digits in /home/3KstP7/prog.php on line 64