<?php
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $number , $word1 , $word2 , $word5 ) {
if ( ( $number % 100 ) <= 20 ) //если два посл. числа <=20, то сравниваем их
$n = $number % 100 ;
else
$n = $number % 10 ; //иначе сравниваем последнее число
if ( $n == 1 )
$text = $word1 ;
elseif ( $n > 1 && $n < 5 )
$text = $word2 ;
else
$text = $word5 ;
return $text . " " ;
}
/*
Преобразует числа от 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 == 0 ) //при нуле сразу пишем результат
$text .= $spelling [ 0 ] . " " ;
else {
if ( $number < 20 ) //1-20 => не режем, а сразу...
$n [ 1 ] = $number ;
else { //больше 21, режем числа на цифры и...
$n [ 1 ] = $number - ( $number % 100 ) ;
$n [ 2 ] = ( $number % 100 ) - ( $number % 10 ) ;
$n [ 3 ] = $number % 10 ;
}
foreach ( $n as $key => $value ) { //... склеиваем слова
if ( $n [ $key ] != 0 ) {
if ( $isFemale && ( $n [ $key ] == 1 || $n [ $key ] == 2 ) ) //для одна/две тысячи
$text .= $femaleSpelling [ $n [ $key ] ] . " " ;
else
$text .= $spelling [ $n [ $key ] ] . " " ;
}
}
}
return $text ;
}
function numberToText( $number ) {
$first = $number % 1000 ;
$second = floor ( $number % 1000000 / 1000 ) ; $third = floor ( $number / 1000000 ) ;
$text = '' ;
if ( $number == 0 )
$text .= smallNumberToText( $number , 0 ) ;
else {
if ( $third > 0 ) {
$text .= smallNumberToText( $third , 0 ) ;
$text .= inclineWord( $third , "миллион" , "миллиона" , "миллионов" ) ;
}
if ( $second > 0 ) {
$text .= smallNumberToText( $second , 1 ) ;
$text .= inclineWord( $second , "тысяча" , "тысячи" , "тысяч" ) ;
}
if ( $first > 0 )
$text .= smallNumberToText( $first , 0 ) ;
}
$text .= inclineWord( $number , "рубль" , "рубля" , "рублей" ) ;
return $text ;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$test = array ( 123456789 , 999999999 , 0 , 1 , 11012013 , 7000008 , 1002 , 1000 , 7000000 , 550 ) ; foreach ( $test as $key => $value ) {
$text = numberToText( $test [ $key ] ) ;
echo $value . " На вашем счету {$text} \n " ;
}
PD9waHAKZXJyb3JfcmVwb3J0aW5nKC0xKTsKbWJfaW50ZXJuYWxfZW5jb2RpbmcoJ3V0Zi04Jyk7CiAKLyog0JLQvtC30LLRgNCw0YnQsNC10YIg0YHQvtC+0YLQstC10YLRgdGC0LLRg9GO0YnRg9GOINGH0LjRgdC70YMg0YTQvtGA0LzRgyDRgdC70L7QstCwOiAxINGA0YPQsdC70YwsIDIg0YDRg9Cx0LvRjywgNSDRgNGD0LHQu9C10LkgKi8KZnVuY3Rpb24gaW5jbGluZVdvcmQoJG51bWJlciwgJHdvcmQxLCAkd29yZDIsICR3b3JkNSkgewoJaWYoKCRudW1iZXIlMTAwKTw9MjApIC8v0LXRgdC70Lgg0LTQstCwINC/0L7RgdC7LiDR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