fork(1) download
  1. <?php
  2.  
  3. // Staring straight up into the sky ... oh my my
  4.  
  5. /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
  6. function inclineWord($number, $word) {
  7. if ($word == "миллион") {
  8. if ($number % 100 != 11 and $number % 10 == 1) {
  9. $word = "миллион";
  10. } elseif ($number % 100 != 12 and $number % 100 != 13 and $number % 100 != 14 and $number % 10 >= 2 and $number % 10 <= 4) {
  11. $word = "миллиона";
  12. } elseif ($number % 10 >= 5 and $number % 10 <= 9 or $number % 10 == 0 or $number % 100 >= 11 and $number % 100 <= 19) {
  13. $word = "миллионов";
  14. }
  15. } elseif ($word == "тысяча") {
  16. if ($number % 100 != 11 and $number % 10 == 1) {
  17. $word = "тысяча";
  18. } elseif ($number % 100 != 12 and $number % 100 != 13 and $number % 100 != 14 and $number % 10 >= 2 and $number % 10 <= 4) {
  19. $word = "тысячи";
  20. } elseif ($number % 10 >= 5 and $number % 10 <= 9 or $number % 10 == 0 or $number % 100 >= 11 and $number % 100 <= 19) {
  21. $word = "тысяч";
  22. }
  23. } elseif ($word == "рубль") {
  24. if ($number % 10 == 1) {
  25. $word = "рубль";
  26. } elseif ($number % 100 != 12 and $number % 100 != 13 and $number % 100 != 14 and $number % 10 >= 2 and $number % 10 <= 4) {
  27. $word = "рубля";
  28. } elseif ($number % 10 >= 5 and $number % 10 <= 9 or $number % 10 == 0 or $number % 100 >= 11 and $number % 100 <= 19) {
  29. $word = "рублей";
  30. }
  31. }
  32. return $word;
  33. }
  34.  
  35. /*
  36.   Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
  37.   если мы считаем число для мужского рода (один рубль),
  38.   и 1 — для женского (одна тысяча)
  39. */
  40. function smallNumberToText($number, $isFemale) {
  41.  
  42. $spelling = array(
  43. 0 => '', 10 => 'десять', 100 => 'сто',
  44. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  45. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  46. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  47. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  48. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  49. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  50. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  51. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  52. 9 => 'девять', 19 => 'девятнадцать'
  53. );
  54.  
  55. $femaleSpelling = array(
  56. 1 => 'одна', 2 => 'две'
  57. );
  58.  
  59. $hundreds = floor($number / 100) * 100;
  60. $twoLastNumber = ($number % 100);
  61. if ($twoLastNumber >= 11 and $twoLastNumber <= 19) {
  62. $array = [];
  63. $array [] = "$hundreds";
  64. $array [] = "$twoLastNumber";
  65. $array1 = [];
  66. foreach($array as $a) {
  67. array_push($array1,$spelling[$a]);
  68. }
  69. } else {
  70. $tens = floor($twoLastNumber / 10) * 10;
  71. $units = $twoLastNumber - $tens;
  72. $array = [];
  73. $array [] = "$hundreds";
  74. $array [] = "$tens";
  75. $array [] = "$units";
  76. $array1 = [];
  77. foreach($array as $a) {
  78. array_push($array1, $spelling[$a]);
  79. }
  80. }
  81. if ($isFemale == 1) {
  82. $array1 = preg_replace('/один/u', 'одна', $array1);
  83. $array1 = preg_replace('/два/u', 'две', $array1);
  84. }
  85. $result = implode(" ", $array1);
  86. $result = trim($result);
  87. return $result;
  88. }
  89.  
  90. function numberToText($number) {
  91. if ($number > 0 and $number <= 999) {
  92. $thirst = $number % 1000;
  93. } elseif ($number >= 1000 and $number <= 999999) {
  94. $second = floor($number / 1000);
  95. $thirst = $number % 1000;
  96. } elseif ($number >= 1000000 - 999999999) {
  97. $first = floor($number / 1000000);
  98. $thirst = $number % 1000;
  99. $second = floor($number % 1000000 - $thirst) / 1000;
  100. }
  101. if ($second % 10 == 1 or $second % 10 == 2) {
  102. $isFemale = 1;
  103. } else {
  104. $isFemale = 0;
  105. }
  106.  
  107. $word1 = "миллион";
  108. $word2 = "тысяча";
  109. $word3 = "рубль";
  110.  
  111. $million = inclineWord($first, $word1);
  112. $thousands = inclineWord($second, $word2);
  113. $rouble = inclineWord($thirst, $word3);
  114.  
  115. $millionNumber = smallNumberToText($first, $isFemale);
  116. $thousandsNumber = smallNumberToText($second, $isFemale);
  117. $handredsNumber = smallNumberToText($thirst, $isFemale);
  118.  
  119. $result = "$millionNumber $million, $thousandsNumber $thousands, $handredsNumber ($number) $rouble\n";
  120. $result = preg_replace('/\\s\\s/u', ' ', $result);
  121. return $result;
  122. }
  123.  
  124. /* Вызовем функцию несколько раз */
  125. $amount1 = mt_rand(1,999999999);
  126. $text1 = numberToText($amount1);
  127.  
  128. echo "На вашем счету: {$text1}\n";
  129.  
  130. $amount2 = mt_rand(1,999999999);
  131. $text2 = numberToText($amount2);
  132.  
  133. echo "На вашем счету: {$text2}\n";
  134.  
  135. $amount3 = mt_rand(1,999999999);
  136. $text3 = numberToText($amount3);
  137.  
  138. echo "На вашем счету: {$text3}\n";
  139.  
  140. $amount4 = mt_rand(1,999999999);
  141. $text4 = numberToText($amount4);
  142.  
  143. echo "На вашем счету: {$text4}\n";
Success #stdin #stdout 0.02s 23776KB
stdin
Standard input is empty
stdout
На вашем счету: девяносто миллионов, триста сорок четыре тысячи, пятьдесят шесть (90344056) рублей

На вашем счету: двести девяносто девять миллионов, триста двадцать семь тысяч, пятьсот двадцать шесть (299327526) рублей

На вашем счету: четыреста тридцать два миллиона, пятьсот девяносто девять тысяч, четыреста пятьдесят семь (432599457) рублей

На вашем счету: триста семьдесят девять миллионов, семьдесят восемь тысяч, девяносто девять (379078099) рублей