PHP utility for converting arbitrary float or integer number to words, mainly in English, Slovak and Czech. These have custom PHP implementation, which will fix INTL/ICU bugs and allows for output customization. Other languages are supported too, but only using INTL/ICU PHP extension, which might be buggy.
- via composer:
$ composer require "lubosdz/number-to-words" : "~1.0.0"
- demo: available only for Slovak language
- repo: https://github.com/lubosdz/number-to-words
// use either factory ..
use lubosdz\numberToWords\NumberToWords;
// .. or language specific implementation
use lubosdz\numberToWords\NumberToWords_SK;
use lubosdz\numberToWords\NumberToWords_CZ;
use lubosdz\numberToWords\NumberToWords_EN;
// Slovensky / Slovak
NumberToWords::convert(123.45, 'sk'); // jednostodvadsaťtri celé štyridsaťpäť
NumberToWords_SK::convert(123.45); // jednostodvadsaťtri celé štyridsaťpäť
NumberToWords_SK::convertIntl(123.45); // jednasto dvasaťtri čiarka štyri päť (ICU returns "dvasať", bug)
// supply number as string - will retain right-sided zeroes on decimal part
NumberToWords_SK::convert(37.40); // tridsaťsedem celé štyri - strip off right-sided zeroes
NumberToWords_SK::convert("37.40"); // tridsaťsedem celé štyridsať - keep right-sided zeroes
NumberToWords_SK::convert("37.400"); // tridsaťsedem celé štyristo - keep right-sided zeroes
// arbitrary decimals word separator
NumberToWords_EN::$txtDecimal = " čiarka ";
NumberToWords_EN::convert(12.30); // dvanásť čiarka tridsať
// with decimals as a fraction - useful in accounting
NumberToWords::$decimalsAsFraction = true;
NumberToWords::convert(123.45, 'sk'); // jednostodvadsaťtri (45/100)
// enforce desired number of decimals
NumberToWords::$numberOfdecimals = 2;
NumberToWords::convert(12.3, 'sk'); // dvanásť celé tridsať
// Česky / Czech:
NumberToWords::convert(123.45, 'cz'); // allowed cz or cs, // sto dvacet tři celá čtyřicet pět
NumberToWords_CZ::convert(123.45); // sto dvacet tři celá čtyřicet pět
NumberToWords_CZ::convertIntl(123.45); // sto dvacet tři celá čtyři pět
// English:
NumberToWords::convert(123.45); // lang code not needed since english is default
NumberToWords_EN::convert(123.45); // one hundred twenty-three point fourty-five
NumberToWords_EN::convertIntl(123.45); // one hundred twenty-three point four five
// arbitrary decimals word separator
NumberToWords_EN::$txtDecimal = " comma ";
NumberToWords_EN::convert(12.30); // twelve comma thirty
// really big number:
NumberToWords_EN::convert(987654321.123);
// nine hundred eighty-seven million, six hundred fifty-four thousand, three hundred twenty-one point one hundred twenty-three
NumberToWords_EN::convertIntl(987654321.123);
// nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one point one two three
// Russian:
NumberToWords::convert(123.45, 'ru'); // сто двадцать три целых сорок пять сотых
// German:
NumberToWords::convert(123.45, 'de'); // einhundertdreiundzwanzig Komma vier fünf
// French:
NumberToWords::convert(123.45, 'fr'); // cent vingt-trois virgule quatre cinq
- support enforcing the number of decimals
- make decimals separator word configurable
- clearify documentation on how to keep right-padded zeroes on decimal part
- added tests
- Support decimals as a fraction
- Fixed zeroes in decimal part on left side
- added unit tests
- Fixed PHP 8.2 compatability
- extended support for any language via INTL/ICU extension
- added factory class
NumberToWords::convert($num, $lang)
- initial release (SK, EN, CZ)