<?php
/**
* 05.05.2015 (20:17)
* TheNumbers.php
* PhpStorm
*/
header ( "Content-Type: text/plain; charset=utf-8" ) ;
/* Делает первую букву предложения заглавной */
function makeFirstLetterUppercase( $input )
{
$result = $firstLetter . $otherLetters ;
return $result ;
}
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
function inclineWord( $input , $word1 , $word2 , $word5 )
{
if ( ( $input >= 11 ) && ( $input <= 14 ) ) {
$result = $word5 ;
} else {
$lastSymbol = $input % 10 ;
if ( $lastSymbol == 1 ) {
$result = $word1 ;
} elseif ( ( $lastSymbol >= 2 ) && ( $lastSymbol <= 4 ) ) {
$result = $word2 ;
} else {
$result = $word5 ;
}
}
return $result ;
}
/**
* Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
* если мы считаем число для мужского рода (один рубль),
* и 1 — для женского (одна тысяча)
*/
function smallNumberToText( $input , $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 => 'две' ,
) ;
if ( $isFemale == 1 ) {
$spelling = array_replace( $spelling , $femaleSpelling ) ;
}
$singleDigits = $input % 10 ;
$tens = $input % 100 ;
$tensSave = $tens - $singleDigits ;
$hundreds = $input - $tens ;
if ( $input == 0 ) {
$text [ ] = $spelling [ $input ] ;
}
if ( $hundreds != 0 ) {
$text [ ] = $spelling [ $hundreds ] ;
}
if ( ( $tens >= 1 ) && ( $tens <= 19 ) ) {
$text [ ] = $spelling [ $tens ] ;
}
if ( ( $tens >= 20 ) && ( $tens <= 99 ) ) {
$text [ ] = $spelling [ $tensSave ] ;
if ( $singleDigits != 0 ) {
$text [ ] = $spelling [ $singleDigits ] ;
}
}
return $result ;
}
function numberToText( $input )
{
$million = floor ( $input / 1000000 ) ; $thousand = floor ( ( $input - ( $million * 1000000 ) ) / 1000 ) ; $hundreds = $input % 1000 ;
if ( $million != 0 ) {
$text [ ] = smallNumberToText( $million , 0 ) ;
$text [ ] = inclineWord( $million , 'миллион' , 'миллиона' , 'миллионов' ) ;
}
if ( $thousand != 0 ) {
$text [ ] = smallNumberToText( $thousand , 1 ) ;
$text [ ] = inclineWord( $thousand , 'тысяча' , 'тысячи' , 'тысяч' ) ;
}
if ( $hundreds != 0 ) {
$text [ ] = smallNumberToText( $hundreds , 0 ) ;
$text [ ] = inclineWord( $hundreds , 'рубль.' , 'рубля.' , 'рублей.' ) ;
}
$result = makeFirstLetterUppercase( $result ) ;
return $result ;
}
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$amount = mt_rand ( 1000000 , 9999999 ) ; $text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$amount = mt_rand ( 10000000 , 99999999 ) ; $text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$amount = 11011011 ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$amount = 11012013 ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$amount = 0 ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$amount = 1 ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
$amount = 9000001 ;
$text = numberToText( $amount ) ;
echo "На вашем счету: {$text} ({$amount} )\n " ;
PD9waHAKCi8qKgogKiAwNS4wNS4yMDE1ICgyMDoxNykKICogVGhlTnVtYmVycy5waHAKICogUGhwU3Rvcm0KICovCgplcnJvcl9yZXBvcnRpbmcoLTEpOwptYl9pbnRlcm5hbF9lbmNvZGluZygndXRmLTgnKTsKaGVhZGVyKCJDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9dXRmLTgiKTsKCi8qINCU0LXQu9Cw0LXRgiDQv9C10YDQstGD0Y4g0LHRg9C60LLRgyDQv9GA0LXQtNC70L7QttC10L3QuNGPINC30LDQs9C70LDQstC90L7QuSAqLwpmdW5jdGlvbiBtYWtlRmlyc3RMZXR0ZXJVcHBlcmNhc2UoJGlucHV0KQp7CiAgICAkZmlyc3RMZXR0ZXIgPSBtYl9zdWJzdHIoJGlucHV0LCAwLCAxKTsKICAgICRmaXJzdExldHRlciA9IG1iX3N0cnRvdXBwZXIoJGZpcnN0TGV0dGVyKTsKICAgICRvdGhlckxldHRlcnMgPSBtYl9zdWJzdHIoJGlucHV0LCAxKTsKICAgICRyZXN1bHQgPSAkZmlyc3RMZXR0ZXIgLiAkb3RoZXJMZXR0ZXJzOwogICAgcmV0dXJuICRyZXN1bHQ7Cn0KCi8qINCS0L7Qt9Cy0YDQsNGJ0LDQtdGCINGB0L7QvtGC0LLQtdGC0YHRgtCy0YPRjtGJ0YPRjiDRh9C40YHQu9GDINGE0L7RgNC80YMg0YHQu9C+0LLQsDogMSDRgNGD0LHQu9GMLCAyINGA0YPQsdC70Y8sIDUg0YDRg9Cx0LvQtdC5ICovC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