Link to home
Start Free TrialLog in
Avatar of Insoftservice inso
Insoftservice insoFlag for India

asked on

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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Can you give us more information on exactly what your input and output will look like.

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?
Avatar of Insoftservice inso

ASKER

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
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?
Please see http://iconoun.com/demo/temp_insoftservice.php

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');

Open in new window

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.
Thanx for ur comments.
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.
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.
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
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please test the script shown in the earlier answer, given here:
https://www.experts-exchange.com/questions/28709051/Number-to-word.html?anchorAnswerId=40950153#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.
Thx both EE script helped me yesterday but i was unable to assign points due to poor net connectivity.
You are welcome.