<?php
// Staring straight up into the sky ... oh my my
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $number , $word1 , $word2 , $word5 ) {
$last2Digits = $number % 100 ;
$lastDigit = $number % 10 ;
if ( $last2Digits >= 10 && $last2Digits <= 19 || $lastDigit == 0 ) {
return $word5 ;
} elseif ( $lastDigit >= 2 && $lastDigit <= 4 ) {
return $word2 ;
} elseif ( $lastDigit == 1 ) {
return $word1 ;
} else {
return $word5 ;
}
return $incline ;
}
/*
Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
если мы считаем число для мужского рода (один рубль),
и 1 — для женского (одна тысяча)
*/
function smallNumberToText( $number , $isFemale ) {
0 => 'ноль' , 10 => 'десять' , 100 => 'сто' ,
1 => 'один' , 11 => 'одиннадцать' , 20 => 'двадцать' , 200 => 'двести' ,
2 => 'два' , 12 => 'двенадцать' , 30 => 'тридцать' , 300 => 'триста' ,
3 => 'три' , 13 => 'тринадцать' , 40 => 'сорок' , 400 => 'четыреста' ,
4 => 'четыре' , 14 => 'четырнадцать' , 50 => 'пятьдесят' , 500 => 'пятьсот' ,
5 => 'пять' , 15 => 'пятнадцать' , 60 => 'шестьдесят' , 600 => 'шестьсот' ,
6 => 'шесть' , 16 => 'шестнадцать' , 70 => 'семьдесят' , 700 => 'семьсот' ,
7 => 'семь' , 17 => 'семнадцать' , 80 => 'восемьдесят' , 800 => 'восемьсот' ,
8 => 'восемь' , 18 => 'восемнадцать' , 90 => 'девяносто' , 900 => 'девятьсот' ,
9 => 'девять' , 19 => 'девятнадцать'
) ;
1 => 'одна' , 2 => 'две'
) ;
$hundreds = $number % 1000 ;
$tens = $number % 100 ;
$digits = $number % 10 ;
$hundredsRound = floor ( $hundreds / 100 ) * 100 ; $tensRound = floor ( $tens / 10 ) * 10 ;
$spelling [ 'null' ] = "" ;
if ( $isFemale == 1 ) {
$spelling [ 1 ] = 'одна' ;
$spelling [ 2 ] = 'две' ;
}
if ( $digits == 0 && ( $tensRound > 10 || $hundredsRound > 100 ) ) {
$digits = 'null' ;
}
if ( $hundredsRound == 0 ) {
$hundredsRound = 'null' ;
}
if ( $tensRound < 10 ) {
$tensRound = 'null' ;
}
if ( $tens >= 10 && $tens <= 19 ) {
$digits = 'null' ;
$tensRound = $tens ;
}
$numbersResult = $spelling [ $hundredsRound ] . ' ' . $spelling [ $tensRound ] . ' ' . $spelling [ $digits ] ;
return $numbersResult ;
}
function numberToText( $number ) {
$millions = floor ( $number / 1000000 ) ; $numberOfMillions = $millions % 1000 ;
$thousands = floor ( $number / 1000 ) ; $numberOfThousands = $thousands % 1000 ;
$numberOfHundreds = $number % 1000 ;
$wordsMillions = smallNumberToText( $numberOfMillions , 0 ) ;
$wordsThousands = smallNumberToText( $numberOfThousands , 1 ) ;
$wordsHundreds = smallNumberToText( $numberOfHundreds , 0 ) ;
$inclineWordsMillions = inclineWord( $numberOfMillions , 'миллион' , 'миллиона' , 'миллионов' ) ;
$inclineWordsThousands = inclineWord( $numberOfThousands , 'тысяча' , 'тысячи' , 'тысяч' ) ;
$inclineWordsHundreds = inclineWord( $numberOfHundreds , 'рубль' , 'рубля' , 'рублей' ) ;
if ( $numberOfThousands == 0 ) {
$wordsThousands = " " ;
$inclineWordsThousands = " " ;
}
if ( $numberOfHundreds == 0 && $number > 0 ) {
$wordsHundreds = " " ;
}
if ( $number < 1000 ) {
$totalResult = $wordsHundreds . ' ' . $inclineWordsHundreds ;
} elseif ( $number < 1000000 ) {
$totalResult = $wordsThousands . ' ' . $inclineWordsThousands . ' ' . $wordsHundreds . ' ' . $inclineWordsHundreds ;
} else {
$totalResult = $wordsMillions . ' ' . $inclineWordsMillions . ' ' . $wordsThousands . ' ' . $inclineWordsThousands . ' ' . $wordsHundreds . ' ' . $inclineWordsHundreds ;
}
return $totalResult ;
}
/* Вызовем функцию несколько раз */
$text1 = numberToText( $amount1 ) ;
echo "На вашем счету {$text1} ($amount1 )\n " ;
$text2 = numberToText( $amount2 ) ;
echo "На вашем счету {$text2} ($amount2 )\n " ;
$text3 = numberToText( $amount3 ) ;
echo "На вашем счету {$text3} ($amount3 )\n " ;
$text4 = numberToText( $amount4 ) ;
echo "На вашем счету {$text4} ($amount4 )\n " ;
PD9waHAKCi8vIFN0YXJpbmcgc3RyYWlnaHQgdXAgaW50byB0aGUgc2t5IC4uLiBvaCBteSBteQplcnJvcl9yZXBvcnRpbmcoLTEpOwptYl9pbnRlcm5hbF9lbmNvZGluZygndXRmLTgnKTsKIAogCi8qINCS0L7Qt9Cy0YDQsNGJ0LDQtdGCINGB0L7QvtGC0LLQtdGC0YHRgtCy0YPRjtGJ0YPRjiDRh9C40YHQu9GDINGE0L7RgNC80YMg0YHQu9C+0LLQsDogMSDRgNGD0LHQu9GMLCAyINGA0YPQsdC70Y8sIDUg0YDRg9Cx0LvQtdC5ICovCmZ1bmN0aW9uIGluY2xpbmVXb3JkKCRudW1iZXIsICR3b3JkMSwgJHdvcmQyLCAkd29yZDUpIHsKCSRsYXN0MkRpZ2l0cyA9ICRudW1iZXIgJSAxMDA7CgkkbGFzdERpZ2l0ID0gJG51bWJlciAlIDEwOwogICAgCiAgICBpZiAoJGxhc3QyRGlnaXRzID49IDEwICYmICRsYXN0MkRpZ2l0cyA8PSAxOSB8fCAkbGFzdERpZ2l0ID09IDAgKSB7CiAgICAJcmV0dXJuICR3b3JkNTsKICAgIH0gZWxzZWlmICgkbGFzdERpZ2l0ID49IDIgJiYgJGxhc3REaWdpdCA8PSA0KSB7CiAgICAJcmV0dXJuICR3b3JkMjsKICAgIH0gZWxzZWlmICgkbGFzdERpZ2l0ID09IDEpIHsKICAgIAlyZXR1cm4gJHdvcmQxOwogICAgfSBlbHNlIHsKICAgIAlyZXR1cm4gJHdvcmQ1OwogICAgfQogICAgCiAgICByZXR1cm4gJGluY2xpbmU7Cn0KLyogCiAgICDQn9GA0LXQvtCx0YDQsNC30YPQtdGCINGH0LjRgdC70LAg0L7RgiAwINC00L4gOTk5INCyINGC0LXQutGB0YIuINC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