ivanovsaleksejs/num-to-text

Converts numbers or prices to text representation in various languages. For example, 437605 becomes "four hundred thirty seven thousand six hundred five". Price class for displaying prices with currencies is also available.

dev-master 2024-04-18 13:14 UTC

This package is auto-updated.

Last update: 2025-06-18 15:57:30 UTC


README

Converts numbers or prices to text representation in various languages.

Supported languages

Currently, library supports these languages:

  • English
  • Russian
  • Latvian

French and Spanish are in progress.

How to install

composer require ivanovsaleksejs/num-to-text:dev-master

or add to your composer.json

{
    "require": {
        "ivanovsaleksejs\num-to-text": "^2.*"
    }
}

and then

composer install

How to use

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

How to add new language

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 and toWords
  • override some other functions of main class if necessary.

How to run unit tests

./vendor/bin/phpunit