Numera is a PHP library that provides a simple and efficient way to convert numbers to words and vice versa. It supports multiple languages and can be easily extended to support more languages.
- Features
- Installation and Setup
- Usage
- Supported Languages
- Set Locale
- Set Locale Fallback
- Get Translates
- Add Translate
- Create a New Language
- Author
- Contributing
- Documentation
- License
- Convert numbers to words (e.g. 1234 to "one thousand two hundred thirty-four")
- Convert words to numbers (e.g. "one thousand two hundred thirty-four" to 1234)
- Support for multiple languages (currently English and Persian, with more to come)
- Camel case support for output words
- Easy to use and extend
You can install Numera using Composer:
composer require pinoox/numera
To use Numera, you need to initialize it with a locale. You can do this using the init
method:
use Pino\Numera;
$numera = Numera::init('en'); // Initialize with English locale
To convert a number to words, use the convertToWords
method:
$result = $numera->convertToWords(4454545156);
echo $result; // Output: "four billion, four hundred fifty-four million, five hundred forty-five thousand, one hundred fifty-six"
Alternatively, you can use the n2w
method for a simpler syntax:
$result = $numera->n2w('4,454,545,156');
echo $result; // Output: "four billion, four hundred fifty-four million, five hundred forty-five thousand, one hundred fifty-six"
To convert a number to summary words, use the convertToSummary
method:
$result = $numera->convertToSummary(4454545156);
echo $result; // Output: "4 Billion, 454 Million, 545 Thousand, 156"
Alternatively, you can use the n2w
method for a simpler syntax:
$result = $numera->n2s('4,454,545,156');
echo $result; // Output: "4 Billion, 454 Million, 545 Thousand, 156"
To convert words to a number, use the convertToNumber
method:
$result = $numera->convertToNumber('four billion, four hundred fifty-four million, five hundred forty-five thousand, one hundred fifty-six');
echo $result; // Output: 4454545156
Alternatively, you can use the w2n
method for a simpler syntax:
$result = $numera->w2n("4 Billion, 454 Million, 545 Thousand, 156");
echo $result; // Output: 4454545156
You can also specify separators for the w2n
method:
$result = $numera->w2n('four billion, four hundred fifty-four million, five hundred forty-five thousand, one hundred fifty-six', [' ', ',']);
echo $result; // Output: 4454545156
To use camel case for output words, use the setCamelCase
method:
$numera->setCamelCase(true);
$result = $numera->convertToWords('4,454,545,156');
echo $result; // Output: "Four Billion, Four Hundred Fifty-Four Million, Five Hundred Forty-Five Thousand, One Hundred Fifty-Six"
Numera currently supports the following languages:
- English (en)
- Persian (fa)
To set the locale for the Numera object, use the setLocale
method:
$numera->setLocale('fa'); // Set locale to Persian
To set the fallback locale for the Numera object, use the setLocaleFallback
method:
$numera->setLocaleFallback('en'); // Set fallback locale to English
To get the translates for the current locale, use the getTranslates
method:
$translates = $numera->getTranslates();
print_r($translates); // Output: Array of translates for the current locale
To add translates for a specific locale, use the addTranslate
method:
$numera->addTranslate('fr', ['four' => 'quatre']); // Add French translates
To add translates by array file for a specific locale, use the addTranslateFile
method:
$numera->addTranslateFile('fr','/path/lang/fr.php'); // Add French translates
If you want to add support for a new language, please read our Translation Guide for a step-by-step guide on how to create a new language pack.
Numera was created by Pinoox.
If you'd like to contribute to Numera, please fork the repository and submit a pull request. We'd love to have your help.
- README.md - Documents
- TranslationGuide.md - Guide for creating a new language pack
Numera is licensed under the MIT License. See the LICENSE file for more information.