# Number to word

I want to change number to words
I tried to use number_word function but it converts even 2 to two .

20,00,000 is twenty lakhs
o/p required 20 Lac
200,00,000 is 2 crore (India)
o/p required 2 Cr
LVL 15
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:

For instance
200,000 is twenty lakhs which will be given to twenty five people

Would such a case exist and would both twenty's need to change.

lakhs => Lac
crore => Cr

Is that also part of the requirement?
Author Commented:
i just want to convert the number to words
input   2,00,000 expected o/p 2 lac
currently i am able to convert it to twenty lakhs

input 2,03,000 expected o/p 2.03 lac
Commented:
These may be western-only but it's worth asking.
Have you looked at this? http://php.net/manual/en/class.numberformatter.php
Or at this? http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/

Starting with this...

2,00,000

... we can see that there are three parts to the value, separated by commas.  Here they are:

2
00
000

To get you a really good answer we would need to know how these numbers should be represented in the resulting string.

I am guessing that the 2 tells us the number of lakhs, up to 99 lakhs, then the 100 lakhs would be expressed as 1 crore?

And the 00 (second position) is simply carried forward, and does not change the expression of lakhs or crore?
Commented:

As usual, the quality of the answer you get is directly related to the quality of the test data you provide.  This code appears to work for the examples we have here, but if there are other examples that you can find that do not work, please post those inputs and expected outputs and we can adjust the function to suit.
``````<?php // demo/temp_insoftservice.php

/**
* http://www.experts-exchange.com/questions/28709051/Number-to-word.html
*/
error_reporting(E_ALL);

function lakhs_crore(\$numstr)
{
// ISOLATE THE PARTS OF THE NUMBER STRING
\$parts = explode(',', \$numstr);

// THIS 3RD PART APPEARS TO BE UNUSED IN ANY OF THE EXAMPLES
unset(\$parts[2]);

// CHOOSE LAKHS OR CRORE, ADJUST MAGNITUDE
\$type = 'Lac';
if (\$parts[0] > 99)
{
\$type = 'Cr';
\$parts[0] = (int) \$parts[0] / 100;
}

// DETERMINE IF FRACTIONAL QUANTITY IS PRESENT
if (!(int)\$parts[1])
{
unset(\$parts[1]);
}

return implode('.', \$parts) . ' ' . \$type;
}

// TEST THE FUNCTION USING ALL OF THE TEST DATA EXAMPLES PROVIDED BY THE AUTHOR OF THE E-E QUESTION
echo '<pre>';
echo PHP_EOL . lakhs_crore('20,00,000');
echo PHP_EOL . lakhs_crore('200,00,000');
echo PHP_EOL . lakhs_crore('2,00,000');
echo PHP_EOL . lakhs_crore('2,03,000');
``````
Commented:
input 2,03,000 expected o/p 2.03 lac
This is more than just a number conversion - you are asking to convert it to a different denomination i.e. it is not the case of convert 2,030,000 to two million thirty thousand - you want to convert from one set of units to another and when doing so it appears you want to retain some numerics i.e. 2.03 is still a number.

Which is why I asked for the rules how does 2,030,000 translate into lac - there is obviously some conversion going on which is important to any solution that is derived.
Author Commented:
Ray there is no such rule for "," user can add data without comma too for eg 203000.

Ya 100 lakh is 1 crore .
Value can be 200 crore too.
Commented:
no such rule for "," user can add data without comma too for eg 203000.
How would we discern whether this means 2,03,000, or 203000 with implied [,00,000]?  Please give us a little more explanation to go on, thanks.
Commented:
Further to Ray's request,

Ya 100 lakh is 1 crore .
Value can be 200 crore too.
What would be really helpful is a complete layout of what the potential data scenarios are - I am sure these are very familiar to you but not to us and trying to put the pieces of this puzzle together is taking time. Po
Commented:
I found the Wikipedia reference.  Maybe this is closer to what you need?
``````<?php // demo/temp_insoftservice.php

/**
* http://www.experts-exchange.com/questions/28709051/Number-to-word.html
*
* https://en.wikipedia.org/wiki/Lakh
*
* 1 Lakh  = 100,000 or 10^5
* 1 Crore = 100 Lakh
*/
error_reporting(E_ALL);

function lakhs_crore(\$numstr)
{
// GET WESTERN NOTATION OF THE NUMBER
\$numstr = preg_replace('/[^0-9-]/', NULL, \$numstr);

// GET THE NUMBER OF LAKHS
\$lakhs = \$numstr / 100000;

// GET THE NUMBER OF CRORE
\$crore = \$lakhs / 100;

// RETURN THE CORRECT DESIGNATION
if (\$crore > 1)
{
\$crore = number_format(\$crore,2);
\$crore = rtrim(\$crore, '0');
\$crore = rtrim(\$crore, '.');
return \$crore . ' Cr';
}

\$lakhs = number_format(\$lakhs,2);
\$lakhs = rtrim(\$lakhs, '0');
\$lakhs = rtrim(\$lakhs, '.');
return \$lakhs . ' Lac';
}

// TEST THE FUNCTION USING ALL OF THE TEST DATA EXAMPLES PROVIDED BY THE AUTHOR OF THE E-E QUESTION
echo '<pre>';
echo PHP_EOL . lakhs_crore('20,00,000');
echo PHP_EOL . lakhs_crore('200,00,000');
echo PHP_EOL . lakhs_crore('2,00,000');
echo PHP_EOL . lakhs_crore('2,03,000');
``````
Author Commented:
Actually in India ,normally last unit for money is crore.
So, we can have 200 crore or 2000 crore.
@Ray its money which is added by user it can with/without comma separated
Commented:
Here is a simplistic sample that can be expanded on to achieve the required output.
Expand the conversion array to include any other conversions you want to output.
``````<?php
\$input = "200,00,000";
\$input = preg_replace('/[^0-9]/', '', \$input);
echo \$input . "<br/>";
\$result = convert(\$input);
echo "{\$result}<br/>";

function convert(\$in)
{
static \$default = 'one';
static \$conversion = array (
10000000 => 'crores',
100000 => 'lakh'
);
foreach(\$conversion as \$c => \$lable) {
if (\$in > \$c) return (float)(\$in / \$c) . " " . \$lable;
}

return \$default;
}
``````
Working sample here

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
http://www.experts-exchange.com/questions/28709051/Number-to-word.html#a40950153

The script is located on my server, here:
http://iconoun.com/demo/temp_insoftservice.php

The output from that script is shown here:
20 Lac
2 Cr
2 Lac
2.03 Lac

Does that script work correctly, giving the output you expect?

If it does, good; we are done.

If it does not, please show us the SSCCE, giving the complete set of test cases so we can get you an answer and move on.  Thanks.
Author Commented:
Thx both EE script helped me yesterday but i was unable to assign points due to poor net connectivity.
Commented:
You are welcome.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.