Converts numbers or prices to text representation in various languages.
Currently, library supports these languages:
- English
- Russian
- Latvian
French and Spanish are in progress.
composer require ivanovsaleksejs/num-to-text:dev-master
or add to your composer.json
{
"require": {
"ivanovsaleksejs\num-to-text": "^2.*"
}
}
and then
composer install
include __DIR__ . '/vendor/autoload.php';
use ivanovsaleksejs\NumToText\Num;
use ivanovsaleksejs\NumToText\Price;
echo Num::toText(1234, 'EN') . "\n";
// Echoes 'one thousand two hundred thirty four'
echo Price::toText(123456.78, [['dollars', 'dollar'], ['cents', 'cent']], 'EN', true) . "\n";
// Echoes 'one hundred twenty three thousand four hundred fifty six dollars 78 cents'
echo Price::toText(123456.78, [['dollars', 'dollar'], ['cents', 'cent']], 'EN') . "\n";
// Echoes 'one hundred twenty three thousand four hundred fifty six dollars seventy eight cents'
echo Price::toText(1.02, [['рублей', 'рубль', 'рубля'], ['копеек', 'копейка', 'копейки']], 'RU', false, false, [0, 1]); . "\n";
// Echoes 'один рубль две копейки'
// Please note, the sixth parameter specifies the gender for the integer and decimal parts of the number respectively
// 0 - masculine
// 1 - feminine
// 2 - neutral
To add a language, you need to:
- extend main class NumToText (to make sure shorthand functions work, add the code of the language in caps after underscore to the name of new class, for example, NumToText_DE)
- define functions
digitToWord
andtoWords
- override some other functions of main class if necessary.
./vendor/bin/phpunit