fork 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. if ($number % 100 >= 11 and $number % 100 <= 19) {
  10. return $word5;
  11. }
  12.  
  13. if ($number % 10 == 1) {
  14. return $word1;
  15. } elseif ($number % 10 >= 2 and $number % 10 <= 4) {
  16. return $word2;
  17. } else {
  18. return $word5;
  19. }
  20. }
  21.  
  22. /*
  23.   Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
  24.   если мы считаем число для мужского рода (один рубль),
  25.   и 1 — для женского (одна тысяча)
  26. */
  27. function smallNumberToText($number, $isFemale) {
  28.  
  29. $spelling = array(
  30. 0 => 'ноль', 10 => 'десять', 100 => 'сто',
  31. 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
  32. 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
  33. 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
  34. 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
  35. 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
  36. 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
  37. 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
  38. 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
  39. 9 => 'девять', 19 => 'девятнадцать'
  40. );
  41.  
  42. $femaleSpelling = array(
  43. 1 => 'одна', 2 => 'две'
  44. );
  45.  
  46. if ($isFemale == true) {
  47. $spelling = array_replace($spelling, $femaleSpelling);
  48. }
  49.  
  50. $words = array();
  51.  
  52. if ($number >= 100) {
  53. $hundreds = floor($number / 100) * 100;
  54. $decades = $number % 100;
  55. $numbers = $decades % 10;
  56.  
  57. if (array_key_exists($number, $spelling)) {
  58. array_push($words, $spelling[$number]);
  59. } elseif (array_key_exists($decades, $spelling)) {
  60. array_push($words, $spelling[$hundreds], $spelling[$decades]);
  61. } else {
  62. $decades = $decades - $numbers;
  63. array_push($words, $spelling[$hundreds], $spelling[$decades], $spelling[$numbers]);
  64. }
  65. } elseif ($number >= 10) {
  66. $decades = floor($number / 10) * 10;
  67. $numbers = $number % 10;
  68. if (array_key_exists($number, $spelling)) {
  69. array_push($words, $spelling[$number]);
  70. } else {
  71. array_push($words, $spelling[$decades], $spelling[$numbers]);
  72. }
  73. } elseif ($number >= 1) {
  74. array_push($words, $spelling[$number]);
  75. }
  76.  
  77. $output = implode(' ', $words);
  78. return $output;
  79. }
  80.  
  81. function numberToText($number) {
  82.  
  83. $constructing = array();
  84. $numbers = $number;
  85. $thousandsForThousands = intval(floor($number / 1000));
  86.  
  87. if ($number > 999999) {
  88. $millions = intval(floor($number / 1000000));
  89. $thousandsForMillions = intval(floor($number % 1000000 / 1000));
  90. $numbersForMillions = $number - ($millions * 1000000) - ($thousandsForMillions * 1000);
  91. $numbers = $numbersForMillions;
  92. $thousandsForThousands = $thousandsForMillions;
  93. $addMilions = smallNumberToText($millions, null).' '.inclineWord($millions, 'миллион', 'миллиона', 'миллионов');
  94. array_push($constructing, $addMilions);
  95. }
  96.  
  97. if ($number > 999) {
  98. $thousands = intval(floor($number / 1000));
  99. $numbersForThousands = $number - ($thousands * 1000);
  100. if ($number < 1000000) {
  101. $numbers = $numbersForThousands;
  102. }
  103. $addThousands = smallNumberToText($thousandsForThousands, 1).' '.inclineWord($thousandsForThousands, 'тысяча', 'тысячи', 'тысяч');
  104.  
  105. if ($thousandsForThousands != 0) {
  106. array_push($constructing, $addThousands);
  107. }
  108. }
  109.  
  110. if ($number > 0) {
  111. $addNumbers = smallNumberToText($numbers, null).' '.inclineWord($numbers, 'рубль', 'рубля', 'рублей')." ({$number})";
  112. array_push($constructing, $addNumbers);
  113. }
  114.  
  115. $total = implode(' ', $constructing);
  116. return $total;
  117. }
  118.  
  119. /* Вызовем функцию несколько раз */
  120. $amount1 = mt_rand(1,99999999);
  121. $text1 = numberToText($amount1);
  122.  
  123. echo "На вашем счету {$text1}\n";
  124.  
  125. $amount2 = mt_rand(1,99999999);
  126. $text2 = numberToText($amount2);
  127.  
  128. echo "На вашем счету {$text2}\n";
  129.  
  130. $amount3 = mt_rand(1,99999999);
  131. $text3 = numberToText($amount3);
  132.  
  133. echo "На вашем счету {$text3}\n";
  134.  
  135. $amount4 = mt_rand(1,99999999);
  136. $text4 = numberToText($amount4);
  137.  
  138. echo "На вашем счету {$text4}\n";
Success #stdin #stdout 0.01s 23840KB
stdin
Standard input is empty
stdout
На вашем счету девятнадцать миллионов семьдесят одна тысяча четыреста девяносто четыре рубля (19071494)
На вашем счету семь миллионов четыреста семьдесят четыре тысячи семьдесят семь рублей (7474077)
На вашем счету тридцать три миллиона четыреста девяносто тысяч шестьсот тридцать пять рублей (33490635)
На вашем счету восемьдесят восемь миллионов четыреста восемьдесят три тысячи триста двадцать рублей (88483320)