Skip to content
/ numera Public

Numera: PHP Number to Words Conversion Library

License

Notifications You must be signed in to change notification settings

pinoox/numera

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Numera: A PHP Library for Number to Words Conversion

Latest Stable Version GitHub Stars GitHub Forks GitHub Issues License Total Downloads

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

  • 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

Installation and Setup

Install via Composer

You can install Numera using Composer:

composer require pinoox/numera

Initialize 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

Convert Numbers to Words

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"

Convert Numbers to Summary

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"

Convert Words to Numbers

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

Use Camel Case

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"

Supported Languages

Numera currently supports the following languages:

  • English (en)
  • Persian (fa)

Set Locale

To set the locale for the Numera object, use the setLocale method:

$numera->setLocale('fa'); // Set locale to Persian

Set Locale Fallback

To set the fallback locale for the Numera object, use the setLocaleFallback method:

$numera->setLocaleFallback('en'); // Set fallback locale to English

Get Translates

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

Add Translate

To add translates for a specific locale, use the addTranslate method:

$numera->addTranslate('fr', ['four' => 'quatre']); // Add French translates

Add Translate File

To add translates by array file for a specific locale, use the addTranslateFile method:

$numera->addTranslateFile('fr','/path/lang/fr.php'); // Add French translates

Create a New Language

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.

Author

Numera was created by Pinoox.

Contributing

If you'd like to contribute to Numera, please fork the repository and submit a pull request. We'd love to have your help.

Documentation

License

Numera is licensed under the MIT License. See the LICENSE file for more information.