<?php
// Staring straight up into the sky ... oh my my
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $number , $word1 , $word2 , $word5 ) {
$lastDigits = $number % 100 ;
if ( ( $lastDigits - $lastDigits % 10 ) / 10 == 1 ) return $word5 ;
if ( $lastDigits % 10 == 1 ) return $word1 ;
if ( $lastDigits % 10 >= 2 && $lastDigits % 10 <= 4 ) return $word2 ;
return $word5 ;
}
/*
Преобразует числа от 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 => 'две'
) ;
$text ;
if ( $number - ( $number % 100 ) != 0 ) $text .= $spelling [ $number - ( $number % 100 ) ] . " " ;
if ( ( $number % 100 <= 20 && $number % 100 >= 3 ) || ( $isFemale == 0 && $number % 100 <= 20 && $number % 100 != 0 ) ) $text .= $spelling [ $number % 100 ] . " " ;
else if ( $number % 100 <= 20 && $number % 100 != 0 ) $text .= $femaleSpelling [ $number % 100 ] . " " ;
else {
if ( ( $number % 100 ) - ( $number % 10 ) != 0 ) $text .= $spelling [ ( $number % 100 ) - ( $number % 10 ) ] . " " ;
if ( $number % 10 != 0 && $isFemale && ( $number % 10 == 1 || $number % 10 == 2 ) ) $text .= $femaleSpelling [ $number % 10 ] . " " ;
else if ( $number % 10 != 0 ) $text .= $spelling [ $number % 10 ] . " " ;
}
return $text ;
}
function numberToText( $number ) {
$text ;
if ( $number == 0 ) return "ноль рублей" ;
if ( $number >= 1000000 && ( $number - $number % 1000000 ) / 1000000 != 0 ) $text .= smallNumberToText( ( $number - $number % 1000000 ) / 1000000 , 0 ) . inclineWord( ( $number - $number % 1000000 ) / 1000000 , "миллион" , "миллиона" , "миллионов" ) . " " ;
if ( $number >= 1000 && ( $number % 1000000 - $number % 1000 ) / 1000 != 0 ) $text .= smallNumberToText( ( $number % 1000000 - $number % 1000 ) / 1000 , 1 ) . inclineWord( ( $number % 1000000 - $number % 1000 ) / 1000 , "тысяча" , "тысячи" , "тысяч" ) . " " ;
if ( $number % 1000 != 0 ) $text .= smallNumberToText( $number % 1000 , 0 ) . inclineWord( $number % 1000 , "рубль" , "рубля" , "рублей" ) . " " ;
else $text .= "рублей" ;
return $text ;
}
/* Вызовем функцию несколько раз */
$amount1 = 1000000 ;
$text1 = numberToText( $amount1 ) ;
echo "На вашем счету {$text1} \n " ;
$text2 = numberToText( $amount2 ) ;
echo "На вашем счету {$text2} \n " ;
$amount3 = 0 ;
$text3 = numberToText( $amount3 ) ;
echo "На вашем счету {$text3} \n " ;
$amount4 = 100000001 ;
$text4 = numberToText( $amount4 ) ;
echo "На вашем счету {$text4} \n " ;
PD9waHAKIAovLyBTdGFyaW5nIHN0cmFpZ2h0IHVwIGludG8gdGhlIHNreSAuLi4gb2ggbXkgbXkKZXJyb3JfcmVwb3J0aW5nKC0xKTsKbWJfaW50ZXJuYWxfZW5jb2RpbmcoJ3V0Zi04Jyk7CiAKIAovKiDQktC+0LfQstGA0LDRidCw0LXRgiDRgdC+0L7RgtCy0LXRgtGB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