<?php
// Staring straight up into the sky ... oh my my
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $number ) {
$result = "" ;
if ( $number % 100 <= 20 ) {
$test = $number % 100 ;
} else {
$test = $number % 10 ;
}
if ( $test == 1 ) {
$result = "рубль" ;
} elseif ( $test >= 2 && $test <= 4 ) {
$result = "рубля" ;
} else {
$result = "рублей" ;
}
$result = " (" . $number . ") " . $result ;
return $result ;
}
/* Склонение миллионов */
function declensionMillion( $millions ) {
$test = $millions % 10 ;
// echo "\n-{$test}-\n";
if ( $test == 1 ) {
return "миллион" ;
} elseif ( $test >= 2 && $test <= 4 ) {
return "миллиона" ;
} else {
return "миллионов" ;
}
}
/* Склонение тысяч */
function declensionThousands( $thousands ) {
$test = $thousands % 10 ;
if ( $test == 1 ) {
return "тысяча" ;
} elseif ( $test >= 2 && $test <= 4 ) {
return "тысячи" ;
} else {
return "тысяч" ;
}
}
/*
Преобразует числа от 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 => 'две'
) ;
$numberStr = "" ;
$hundred = 0 ; // сотни
$tens = 0 ; // десятки
$units = 0 ; // единицы
if ( $isFemale == 1 ) {
if ( isset ( $femaleSpelling [ $number ] ) ) { return $femaleSpelling [ $number ] ;
}
}
// Проверим можем сразу перевести?
if ( isset ( $spelling [ $number ] ) ) { $numberStr = $spelling [ $number ] ;
} else {
// Если нет. Бъем на составляющие и переводим.
$hundred = floor ( $number / 100 ) ; $tens = $number % 100 ;
$units = $number % 10 ;
if ( $hundred != 0 ) {
$numberStr = $spelling [ $hundred * 100 ] . " " ;
}
if ( isset ( $spelling [ $tens ] ) ) { $numberStr = $numberStr . $spelling [ $tens ] . " " ;
} else {
$numberStr = $numberStr . $spelling [ $tens - $units ] . " " ;
if ( isset ( $femaleSpelling [ $units ] ) && $isFemale == 1 ) { $numberStr = $numberStr . $femaleSpelling [ $units ] ;
} else {
$numberStr = $numberStr . $spelling [ $units ] ;
}
}
}
return $numberStr ;
}
function numberToText( $number ) {
// Ограничение: 999 999 999
$result = "" ;
$millions = floor ( $number / 1000000 ) ; $thousands = fmod ( floor ( $number / 1000 ) , 1000 ) ; $hundreds = fmod ( $number , 1000 ) ;
$millionsStr = "" ;
$thousandsStr = "" ;
$hundredsStr = "" ;
if ( $number == 0 ) {
$hundredsStr = smallNumberToText( $hundreds , 0 ) ;
} else {
if ( $millions != 0 ) {
$millionsStr = smallNumberToText( $millions , 0 ) ;
$millionsStr = $millionsStr . " " . declensionMillion( $millions ) ;
}
if ( $thousands != 0 ) {
$thousandsStr = smallNumberToText( $thousands , 1 ) ;
$thousandsStr = $thousandsStr . " " . declensionThousands( $thousands ) ;
if ( $hundreds != 0 ) {
$hundredsStr = smallNumberToText( $hundreds , 0 ) ;
}
} elseif ( $hundreds != 0 ) {
$hundredsStr = smallNumberToText( $hundreds , 0 ) ;
}
}
$result = trim ( preg_replace ( '/\s{2,}/u' , ' ' , $millionsStr . " " . $thousandsStr . " " . $hundredsStr . inclineWord
( $number ) ) ) ; return $result ;
}
/* Вызовем функцию несколько раз */
$text1 = numberToText( $amount1 ) ;
echo "На вашем счету: {$text1} \n " ;
$text2 = numberToText( $amount2 ) ;
echo "На вашем счету: {$text2} \n " ;
$text3 = numberToText( $amount3 ) ;
echo "На вашем счету: {$text3} \n " ;
$text4 = numberToText( $amount4 ) ;
echo "На вашем счету: {$text4} \n " ;
$text4 = numberToText( $amount4 ) ;
echo "На вашем счету: {$text4} \n " ;
?>
PD9waHAKCS8vIFN0YXJpbmcgc3RyYWlnaHQgdXAgaW50byB0aGUgc2t5IC4uLiBvaCBteSBteQoJZXJyb3JfcmVwb3J0aW5nKC0xKTsKCW1iX2ludGVybmFsX2VuY29kaW5nKCd1dGYtOCcpOwoJIAoJLyog0JLQvtC30LLRgNCw0YnQsNC10YIg0YHQvtC+0YLQstC10YLRgdGC0LLRg9GO0YnRg9GOINGH0LjRgdC70YMg0YTQvtGA0LzRgyDRgdC70L7QstCwOiAxINGA0YPQsdC70YwsIDIg0YDRg9Cx0LvRjywgNSDRgNGD0LHQu9C10LkgKi8KCWZ1bmN0aW9uIGluY2xpbmVXb3JkKCRudW1iZXIpIHsKCQkkcmVzdWx0ID0gIiI7CgkJaWYoJG51bWJlciUxMDAgPD0gMjApewoJCQkkdGVzdCA9ICRudW1iZXIlMTAwOwoJCX0gZWxzZSB7CgkJCSR0ZXN0ID0gJG51bWJlciUxMDsKCQl9CgkJaWYoJHRlc3QgPT0gMSl7CgkJCSRyZXN1bHQgPSAi0YDRg9Cx0LvRjCI7CgkJfSBlbHNlaWYoJHRlc3QgPj0gMiAmJiAkdGVzdCA8PSA0KSB7CgkJCSRyZXN1bHQgPSAi0YDRg9Cx0LvRjyI7CgkJfSBlbHNlIHsKCQkJJHJlc3VsdCA9ICLRgNGD0LHQu9C10LkiOwoJCX0KCQkkcmVzdWx0ID0gIiAoIi4kbnVtYmVyLiIpICIuJHJlc3VsdDsKCQlyZXR1cm4gJHJlc3VsdD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