kwn / number-to-words
Multi language standalone PHP number to words converter. Fully tested, open for extensions and new languages.
Installs: 3 324 666
Dependents: 27
Suggesters: 1
Security: 0
Stars: 383
Watchers: 15
Forks: 157
Open Issues: 8
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.7.2
- dev-master
- 2.11.0
- 2.10.0
- 2.9.1
- 2.9.0
- 2.8.0
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.4
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.0
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.1
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.0
- 1.10.1
- 1.10.0
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.7
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-major-refactoring
- dev-russian-language-refactoring
This package is auto-updated.
Last update: 2024-11-04 21:13:54 UTC
README
Welcome to number-to-words
, a PHP utility that seamlessly transforms numeric values into their corresponding words. Effortlessly convert numbers, such as 123, into expressive and readable formats like "one hundred and twenty-three" with just a few lines of code.
Installation
Add package to your composer.json by running:
$ composer require kwn/number-to-words
Usage
There are two types of number-to-words transformation: number and currency. In order to use a relevant transformer for specific language create an instance of NumberToWords
class and call a method that creates a new instance of the desired transformer;
Number Transformer
Create a transformer for specific language using the getNumberTransformer('lang')
method:
use NumberToWords\NumberToWords; // create the number to words "manager" class $numberToWords = new NumberToWords(); // build a new number transformer using the RFC 3066 language identifier $numberTransformer = $numberToWords->getNumberTransformer('en');
Transformer can be used by passing in numeric values to the toWords()
method:
$numberTransformer->toWords(5120); // outputs "five thousand one hundred twenty"
It can be also used with a static method:
NumberToWords::transformNumber('en', 5120); // outputs "five thousand one hundred twenty"
Currency Transformer
Creating a currency transformer works just like a number transformer.
use NumberToWords\NumberToWords; // create the number to words "manager" class $numberToWords = new NumberToWords(); // build a new currency transformer using the RFC 3066 language identifier $currencyTransformer = $numberToWords->getCurrencyTransformer('en');
Then it can be used passing in numeric values for amount and ISO 4217 currency identifier to the toWords()
method:
$currencyTransformer->toWords(5099, 'USD'); // outputs "fifty dollars ninety nine cents"
It can be also used with a static method:
NumberToWords::transformCurrency('en', 5099, 'USD'); // outputs "fifty dollars ninety nine cents"
Note: The Currency Transformer within this library processes integers; ensure your input amounts are in whole numbers by multiplying any float values by 100 before transformation. For instance, transform 45.67 by using 4567 as the input for accurate currency conversion.
Available locale
Contributors
Some transformers were ported from the pear/Numbers_Words
library. Others were created by contributors. Thank you!
Version 2.x - BC and major changes
- Dropped support for PHP <7.4.
- Added typehints for
NumberTransformer
andCurrencyTransformer
interfaces. Now both accept integer numbers only (Albanian language might be affected). - Added support for PSR12.
Questions and answers
Q: What should I do if I encounter a bug while using the library?
A: If you come across a bug, please open an issue on our GitHub repository. As I may not be proficient in all languages, we encourage users to submit fixes and collaborate to enhance the library's functionality.
Q: My language is missing. Could it be added?
A: There's a high chance I don't know your language. Feel free to implement the missing language and open a pull request. You can use the existing languages as a reference.