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