This class converts numbers to Indian-English Words. Works for all kinds of numbers including floats. A few examples.
10,000,00
- Ten Lakh3,478
- Three Thousand Four Hundred And Seventy Eight1,234,567,890
- One Hundred And Twenty Three Crore Forty Five Lakh Sixty Seven Thousand Eight Hundred And Ninty5,024.78
- Five Thousand Twenty Four And 78/100
Navigate to your project directory and execute
composer require ntwindia/ntwindia
If you haven't already, include the composer autoload.php
file.
require_once 'vendor/autoload.php';
Clone this repository using git
git clone git@github.com:swashata/php-number-to-word-india.git
Include the class
<?php
require_once 'src/NTWIndia.php';
require_once 'src/Exception/NTWIndiaInvalidNumber.php';
require_once 'src/Exception/NTWIndiaNumberOverflow.php';
Create an instance
<?php
$ntw = new \NTWIndia\NTWIndia();
Convert values
<?php
echo $ntw->numToWord( 3104007200 );
// Will print Three Hundred Ten Crore Forty Lakh Seven Thousand Two Hundred
If your number is always less than 100, then use numToWordSmall
method to
reduce memory usage.
<?php
echo $ntw->numToWordSmall( 99 );
// Will print Ninty Nine
Two methods are available
Converts any number to word including decimal values. Decimals are converted as
... And 986/1000
. You will mostly need to call this method.
$number
: integer|float The number whose word value would be generated.
Returns string
The word value of the number. All of the words have uppercased
first letter.
Converts numbers smaller than 100 into words. If the number is greater than 99
then it simply calls numToWord
.
Use this when you know your number is lesser than 100 to reduce memory usage.
$number
: integer The number whose word value would be generated. It does not accepts floating values and will convert to integer if given.
Returns string
The word value of the number. All of the words have uppercased
first letter.
Two exceptions are thrown depending on the condition.
If you pass a variable to a method which isn't a valid number ( ! is_numeric( $number ) )
If pass a number which exceeds the limit.
- For
numToWord
it is thePHP_MAX_INT
. - For
numToWordSmall
it is99
.
To translate, kindly replace the public variables:
$hundred
: Hundred word. Defaults 'Hundred'.$thousand
: Thousand word. Defaults 'Thousand'.$lakh
: Lakh word. Defaults 'Lakh'.$crore
: Crore word. Defaults 'Crore'.$and
: And word. Defaults 'And'.$numToWord
: Contains mapping of numbers to words. From0
to20
and multiple of10
upto90
.
Unit test is available through composer.
$ cd /path/to/php-number-to-word-india/
$ composer update
$ vendor/bin/phpunit
It will output something like this.
You are more than welcome to add more test cases and send me a pull request.
Kindly follow Contribution Guide
PHP Number to Word in Indian Style is licensed under GPLv3
Copyright (C) 2017 Swashata Ghosh
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>