<?php
//Число прописью
// Staring straight up into the sky ... oh my my
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $number , $word1 , $word2 , $word5 ) {
/* DIY */
$last2Digits = $number % 100 ;
$lastDigit = $number % 10 ;
if ( ( $last2Digits > 5 && $last2Digits < 20 ) || ( $lastDigit == 0 || ( $lastDigit >= 5 && $lastDigit <= 9 ) ) ) {
return $word5 ;
}
if ( $lastDigit >= 2 && $lastDigit <= 4 ) {
return $word2 ;
}
return $word1 ;
}
/*
Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
если мы считаем число для мужского рода (один рубль),
и 1 — для женского (одна тысяча)
*/
function smallNumberToText( $number , $isFemale ) {
$spelling = [
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 => 'девятнадцать'
] ;
$femaleSpelling = [
1 => 'одна' , 2 => 'две'
] ;
/* DIY */
$digits = [ ] ;
if ( $number == 0 ) {
return "ноль" ;
}
$ones = $number % 10 ;
$tens = $number % 100 ;
$hundreds = $number - $tens ;
if ( $hundreds > 0 ) {
$digits [ ] = $spelling [ $hundreds ] ;
}
if ( $tens >= 10 && $tens < 20 ) {
$digits [ ] = $spelling [ $tens ] ;
}
elseif ( $tens >= 20 ) {
$digits [ ] = $spelling [ $tens - $ones ] ;
}
if ( $ones != 0 ) {
if ( $tens <= 10 || $tens >= 20 ) {
if ( $isFemale == 1 && ( $ones == 1 || $ones == 2 ) ) {
$digits [ ] = $femaleSpelling [ $ones ] ;
}
else {
$digits [ ] = $spelling [ $ones ] ;
}
}
}
return $result ;
}
function numberToText( $number ) {
/* DIY */
if ( $number == 0 )
return "0 (0) рублей" ;
$resultPieces = [ ] ;
$ones = $number % 1000 ;
$thousands = floor ( $number % 1000000 / 1000 ) ; $millions = floor ( $number % 1000000000 / 1000000 ) ;
if ( $millions > 0 ) {
$resultPieces [ ] = smallNumberToText( $millions , 0 ) ;
$resultPieces [ ] = inclineWord( $millions , "миллион" , "миллиона" , "миллионов" ) ;
}
if ( $thousands > 0 ) {
$resultPieces [ ] = smallNumberToText( $thousands , 1 ) ;
$resultPieces [ ] = inclineWord( $thousands , "тысяча" , "тысячи" , "тысяч" ) ;
}
if ( $ones > 0 ) {
$resultPieces [ ] = smallNumberToText( $ones , 0 ) ;
$resultPieces [ ] = " (" . $number . ") " . inclineWord( $ones , "рубль" , "рубля" , "рублей" ) ;
} else {
$resultPieces [ ] = " (" . $number . ") рублей" ;
}
$text = implode ( " " , $resultPieces ) ; return $text ;
}
// Вызовем функцию несколько раз
$text1 = numberToText( $amount1 ) ;
echo "На вашем счету {$text1} \n " ;
$text2 = numberToText( $amount2 ) ;
echo "На вашем счету {$text2} \n " ;
$text3 = numberToText( $amount3 ) ;
echo "На вашем счету {$text3} \n " ;
$text4 = numberToText( $amount4 ) ;
echo "На вашем счету {$text4} \n " ;
$testData = [ 999999999 , 0 , 1 , 11012013 , 7000008 , 1002 , 1000 , 7000000 , 1000001 , 550 ] ;
foreach ( $testData as $data ) {
$text = numberToText( $data ) ;
echo "На вашем счету {$text} \n " ;
}
PD9waHAKCi8v0KfQuNGB0LvQviDQv9GA0L7Qv9C40YHRjNGOCi8vIFN0YXJpbmcgc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D0LHQu9GMKSwKICAgINC4IDEg4oCUINC00LvRjyDQttC10L3RgdC60L7Qs9C+ICjQvtC00L3QsCDRgtGL0YHRj9GH0LApCiovCmZ1bmN0aW9uIHNtYWxsTnVtYmVyVG9UZXh0KCRudW1iZXIsICRpc0ZlbWFsZSkgewoKICAgICRzcGVsbGluZyA9IFsKICAgICAgICAwICAgPT4gICfQvdC+0LvRjCcsICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwICA9PiAgJ9C00LXRgdGP0YLRjCcsICAgICAgIDEwMCA9PiAgJ9GB0YLQvicsCiAgICAgICAgMSAgID0+ICAn0L7QtNC40L0nLCAgICAgICAgIDExICA9PiAgJ9C+0LTQuNC90L3QsNC00YbQsNGC0YwnLCAgICAgIDIwICA9PiAgJ9C00LLQsNC00YbQsNGC0YwnLCAgICAgMjAwID0+ICAn0LTQstC10YHRgtC4JywKICAgICAgICAyICAgPT4gICfQtNCy0LAnLCAgICAgICAgICAxMiAgPT4gICfQtNCy0LXQvdCw0LTRhtCw0YLRjCcsICAgICAgIDMwICA9PiAgJ9GC0YDQuNC00YbQsNGC0YwnLCAgICAgMzAwID0+ICAn0YLRgNC40YHRgtCwJywKICAgICAgICAzICAgPT4gICfRgtGA0LgnLCAgICAgICAgICAxMyAgPT4gICfRgtGA0LjQvdCw0LTRhtCw0YLRjCcsICAgICAgIDQwICA9PiAgJ9GB0L7RgNC+0LonLCAgICAgICAgNDAwID0+ICAn0YfQtdGC0YvRgNC10YHRgtCwJywKICAgICAgICA0ICAgPT4gICfRh9C10YLRi9GA0LUnLCAgICAgICAxNCAgPT4gICfRh9C10YLRi9GA0L3QsNC00YbQsNGC0YwnLCAgICAgNTAgID0+ICAn0L/Rj9GC0YzQtNC10YHRj9GCJywgICAgNTAwID0+ICAn0L/Rj9GC0YzRgdC+0YInLAogICAgICAgIDUgICA9PiAgJ9C/0Y/RgtGMJywgICAgICAgICAxNSAgPT4gICfQv9GP0YLQvdCw0LTRhtCw0YLRjCcsICAgICAgIDYwICA9PiAgJ9GI0LXRgdGC0YzQtNC10YHRj9GCJywgICA2MDAgPT4gICfRiNC10YHRgtGM0YHQvtGCJywKICAgICAgICA2ICAgPT4gICfRiNC10YHRgtGMJywgICAgICAgIDE2ICA9PiAgJ9GI0LXRgdGC0L3QsNC00YbQsNGC0YwnLCAgICAgIDcwICA9PiAgJ9GB0LXQvNGM0LTQtdGB0Y/RgicsICAgIDcwMCA9PiAgJ9GB0LXQvNGM0YHQvtGCJywKICAgICAgICA3ICAgPT4gICfRgdC10LzRjCcsICAgICAgICAgMTcgID0+ICAn0YHQtdC80L3QsNC00YbQsNGC0YwnLCAgICAgICA4MCAgPT4gICfQstC+0YHQtdC80YzQtNC10YHRj9GCJywgICA4MDAgPT4gICfQstC+0YHQtdC80YzRgdC+0YInLAogICAgICAgIDggICA9PiAgJ9Cy0L7RgdC10LzRjCcsICAgICAgIDE4ICA9PiAgJ9Cy0L7RgdC10LzQvdCw0LTRhtCw0YLRjCcsICAgICA5MCAgPT4gICfQtNC10LLRj9C90L7RgdGC0L4nLCAgICAgOTAwID0+ICAn0LTQtdCy0Y/RgtGM0YHQvtGCJywKICAgICAgICA5ICAgPT4gICfQtNC10LLRj9GC0YwnLCAgICAgICAxOSAgPT4gICfQtNC10LLRj9GC0L3QsNC00YbQsNGC0YwnCiAgICBdOwoKICAgICRmZW1hbGVTcGVsbGluZyA9IFsKICAgICAgICAxICAgPT4gICfQvtC00L3QsCcsICAgICAgICAyICAgPT4gICfQtNCy0LUnCiAgICBdOwoKICAgIC8qIERJWSAqLwogICAgJGRpZ2l0cyA9IFtdOwoKICAgIGlmICgkbnVtYmVyID09IDApIHsKICAgICAgICByZXR1cm4gItC90L7Qu9GMIjsKICAgIH0KCiAgICAkb25lcyA9ICRudW1iZXIgJSAxMDsKICAgICR0ZW5zID0gJG51bWJlciAlIDEwMDsKICAgICRodW5kcmVkcyA9ICRudW1iZXIgLSAkdGVuczsKCiAgICBpZiAoJGh1bmRyZWRzID4gMCkgewogICAgICAgICRkaWdpdHNbXSA9ICRzcGVsbGluZ1skaHVuZHJlZHNdOwogICAgfQoKICAgIGlmICgkdGVucyA+PSAxMCAmJiAkdGVucyA8IDIwKSB7CiAgICAgICAgJGRpZ2l0c1tdID0gJHNwZWxsaW5nWyR0ZW5zXTsKICAgIH0KICAgIGVsc2VpZiAoJHRlbnMgPj0gMjApIHsKICAgICAgICAkZGlnaXRzW10gPSAkc3BlbGxpbmdbJHRlbnMgLSAkb25lc107CiAgICB9CgogICAgaWYgKCRvbmVzICE9IDApIHsKICAgICAgICBpZiAoJHRlbnMgPD0gMTAgfHwgJHRlbnM+PSAyMCkgewogICAgICAgICAgICBpZiAoJGlzRmVtYWxlID09IDEgJiYgKCRvbmVzID09IDEgfHwgJG9uZXMgPT0gMikpIHsKICAgICAgICAgICAgICAgICRkaWdpdHNbXSA9ICRmZW1hbGVTcGVsbGluZ1skb25lc107CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICAkZGlnaXRzW10gPSAkc3BlbGxpbmdbJG9uZXNdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgJHJlc3VsdCA9IGltcGxvZGUoIiAiLCAkZGlnaXRzKTsKCiAgICByZXR1cm4gJHJlc3VsdDsKfQoKCmZ1bmN0aW9uIG51bWJlclRvVGV4dCgkbnVtYmVyKSB7CgogICAgLyogRElZICovCiAgICBpZiAoJG51bWJlciA9PSAwKQogICAgICAgIHJldHVybiAiMCAoMCkg0YDRg9Cx0LvQtdC5IjsKCiAgICAkcmVzdWx0UGllY2VzID0gW107CgogICAgJG9uZXMgPSAkbnVtYmVyICUgMTAwMDsKICAgICR0aG91c2FuZHMgPSBmbG9vcigkbnVtYmVyICUgMTAwMDAwMCAvIDEwMDApOwogICAgJG1pbGxpb25zID0gZmxvb3IoJG51bWJlciAlIDEwMDAwMDAwMDAgLyAxMDAwMDAwKTsKCiAgICBpZiAoJG1pbGxpb25zID4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