fork download
  1. <?php
  2.  
  3. // Staring straight up into the sky ... oh my my
  4.  
  5. function spellSmallNumber($num, $female) {
  6. $result = array();
  7. $spelling = array(
  8. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  9. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  10. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  11. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  12. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  13. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  14. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  15. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  16. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  17. 9 => 'девять', 19 => 'девятнадцать'
  18. );
  19.  
  20. $femaleSpelling = array(
  21. 1 => 'одна', 2 => 'две'
  22. );
  23.  
  24. if ($num == 0) {
  25. return $num;
  26. }
  27.  
  28.  
  29. $hundWithoutDec = floor($num / 100) * 100; //убирает десятки у сотен
  30. if($hundWithoutDec != 0) {
  31. array_push($result, $spelling[$hundWithoutDec]);
  32. }
  33. $hundDec = $num % 100; // оставляет только десятки
  34. if($hundDec >= 11 and $hundDec <=19) {
  35. array_push($result, $spelling[$hundDec]);
  36. } elseif($hundDec != 0) {
  37. $hundDecWithoutOne = floor($hundDec / 10) * 10; //десятки без единиц
  38. if($hundDecWithoutOne != 0) {
  39. array_push($result, $spelling[$hundDecWithoutOne]);
  40. }
  41.  
  42. $hundOne = $hundDec % 10;
  43.  
  44. if ($female == 1 or $female == 2) {
  45. array_push($result, $femaleSpelling[$hundOne]);
  46. } elseif($female == 0) {
  47. if ($hundOne != 0) {
  48. array_push($result, $spelling[$hundOne]);
  49. }
  50. }
  51.  
  52. }
  53.  
  54.  
  55.  
  56. $text = implode(' ', $result);
  57. return $text;
  58. }
  59.  
  60.  
  61.  
  62. function getWordForm($num, $word, $word1, $word2) {
  63. $x = $num % 100;
  64. if($x >= 11 and $x <=20){
  65. return $word2;
  66. }
  67. else {
  68. $z = $num % 10;
  69. if($z == 1){
  70. return $word; // тысяча, миллион
  71. }
  72. $z = $num % 10;
  73. if($z >= 2 && $z <= 4){
  74. return $word1; //тысячи, миллиона
  75. }
  76. else{
  77. return $word2; // тысяч, миллионов
  78. }
  79. }
  80. }
  81.  
  82. function inclineWord($number, $word1, $word2, $word5) {
  83. $x = $number % 100;
  84. if($x >= 11 and $x <=19){
  85. return $word5;
  86. } else {
  87. $last2Digits = $number % 10;
  88. if($last2Digits == 1) {
  89. return $word1;
  90. } elseif($last2Digits >= 2 and $last2Digits <= 4) {
  91. return $word2;
  92. } else {
  93. return $word5;
  94. }
  95. }
  96. }
  97.  
  98.  
  99. function spellNumber($number) {
  100. $millions = floor($number / 1000000); //миллионы
  101. $hundredThous = floor($number / 1000);//без тысяч сотни тысяч
  102. $hundWithoutMil = $hundredThous % 1000; // сотни тысяч
  103. $hundred = $number % 1000; //просто сотни
  104.  
  105. $fem = $hundWithoutMil % 10;
  106. if($fem == 1) {
  107. $female = 1;
  108. } elseif ($fem == 2) {
  109. $female = 2;
  110. } else {
  111. $female = 0;
  112. }
  113.  
  114.  
  115. $numberToString = array();
  116. if($millions !=0){
  117. $mill = spellSmallNumber($millions,NULL);
  118. array_push($numberToString,$mill ,NULL);
  119. $milWord = getWordForm($millions,"миллион","миллиона","миллионов");
  120. array_push($numberToString, $milWord);
  121. }
  122.  
  123. if($hundWithoutMil != 0){
  124. $thous = spellSmallNumber($hundWithoutMil,$female);
  125. array_push($numberToString, $thous);
  126. $thousWord = getWordForm($hundWithoutMil,"тысяча","тысячи","тысяч");
  127. array_push($numberToString, $thousWord);
  128. }
  129.  
  130. $hund = spellSmallNumber($hundred,NULL);
  131. array_push($numberToString, $hund);
  132. $hundRouble = inclineWord($hundred,"рубль","рубля","рублей");
  133. array_push($numberToString, $hundRouble);
  134.  
  135. $end = implode(" ", $numberToString);
  136. return $end;
  137. }
  138.  
  139. $amount1 = mt_rand(1,99999999);
  140. $text1 = spellNumber($amount1);
  141.  
  142. echo "На вашем счету {$text1}\n";
  143. echo "($amount1)\n";
  144.  
  145. $amount2 = mt_rand(1,99999999);
  146. $text2 = spellNumber($amount2);
  147.  
  148.  
  149. echo "На вашем счету {$text2}\n";
  150. echo "($amount2)\n";
  151.  
  152. $amount3 = mt_rand(1,99999999);
  153. $text3 = spellNumber($amount3);
  154.  
  155.  
  156. echo "На вашем счету {$text3}\n";
  157. echo "($amount3)\n";
  158.  
  159. $amount4 = mt_rand(1,99999999);
  160. $text4 = spellNumber($amount4);
  161.  
  162. echo "На вашем счету {$text4}\n";
  163. echo "($amount4)\n";
  164.  
  165. $amount5 = 00000001;
  166. $text5 = spellNumber($amount5);
  167.  
  168. echo "На вашем счету {$text5}\n";
  169. echo "($amount5)\n";
  170.  
  171. $amount6 = 10000001;
  172. $text6 = spellNumber($amount6);
  173.  
  174. echo "На вашем счету {$text6}\n";
  175. echo "($amount6)\n";
  176.  
  177. $amount7 = 02020200;
  178. $text7 = spellNumber($amount7);
  179.  
  180. $amount8 = 2020200;
  181. $text8 = spellNumber($amount8);
  182.  
  183. echo "На вашем счету {$text8}\n";
  184. echo "($amount8)\n";
  185.  
  186. $amount9 = 00000012;
  187. $text9 = spellNumber($amount9);
  188.  
  189. echo "На вашем счету {$text9}\n";
  190. echo "($amount9)\n";
  191.  
  192. ?>
Success #stdin #stdout 0.02s 23560KB
stdin
Standard input is empty
stdout
На вашем счету семнадцать  миллионов сто девяносто восемь тысяч девятьсот девяносто два рубля
(17198992)
На вашем счету девяносто шесть  миллионов сто пятьдесят девять тысяч триста восемьдесят девять рублей
(96159389)
На вашем счету сорок шесть  миллионов триста сорок две тысячи восемьсот девяносто четыре рубля
(46342894)
На вашем счету шестьдесят  миллионов сто пятьдесят тысяч двести восемьдесят рублей
(60150280)
На вашем счету один рубль
(1)
На вашем счету десять  миллионов один рубль
(10000001)
На вашем счету два  миллиона двадцать тысяч двести рублей
(2020200)
На вашем счету десять рублей
(10)