### Thread: Spell Number to Words

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2007
Location
kuala lumpur
Posts
19
Rep Power
0

#### Spell Number to Words

Hi all,

I want to spell this number:
example USD 123456.20
one hundred twenty three thousand four hundred fifty five dollar and twenty cent only

anybody out there have write up the function? will appreciate if you can share here.
2. hehe, remind me one of the basic exercice we were used to have when learning c language in old times...

I had so much fun with it that I will let you think about it a bit
3. I will let you think about it a bit
that's very cruel of you .. he doesn't want to think on it therefore he came here to get it fast so if you have it then give it to him
4. Somebody provided a function to do this a long while ago. Have you searched the forums?
5. Originally Posted by m4zw4n
Hi all,

I want to spell this number:
example USD 123456.20
one hundred twenty three thousand four hundred fifty five dollar and twenty cent only

anybody out there have write up the function? will appreciate if you can share here.
hi , there m4zw4n

use this , you can get the logic:

Code:
```\$lakhs = floor(\$number / 100000);  /* lakhs (giga) */
\$number -= \$lakhs * 100000;

\$thousands = floor(\$number / 1000);     /* Thousands (kilo) */
\$number -= \$thousands * 1000;
\$hundreds = floor(\$number / 100);      /* Hundreds (hecto) */
\$number -= \$hundreds * 100;
\$tens = floor(\$number / 10);       /* Tens (deca) */
\$ones = \$number % 10;               /* Ones */
\$res = "";

//echo "<hr>".\$lakhs;

if (\$lakhs){

\$res .= \$this->numToWords(\$lakhs) ;
\$res.=(\$lakhs>10)?" Lakhs":" Lakh";
}

if(\$thousands){
\$res .= (empty(\$res) ? "" : " ") .
\$this->numToWords(\$thousands) . " Thousand";
}

if (\$hundreds){
\$res .= (empty(\$res) ? "" : " ") .
\$this->numToWords(\$hundreds) . " Hundred";
}

\$arr_ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",
"Nineteen");
\$arr_tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",
"Seventy", "Eigthy", "Ninety");

if (\$tens || \$ones){
if (!empty(\$res)){
\$res .= " and ";
}

if (\$tens < 2){
\$res .= \$arr_ones[\$tens * 10 + \$ones];
}
else{
\$res .= \$arr_tens[\$tens];
if (\$ones){
\$res .= "-" . \$arr_ones[\$ones];
}
}
}

if (empty(\$res)){
\$res = "zero";
}```
6. Blah that's not fun... there is even a version publish as comment in php manual =(

Btw, shouldn't "Eightteen" be either "Eight teens" or "Eighteen" ?
7. I just wrote something that handles the problem a little differently. It takes two functions in order to complete, but it handles the input as a string instead of a number, so all the processing is done on characters. This allows unreasonably large numbers to be processed easily.

Now, the only limitation is that I haven't bothered to copy and paste all the designators and suffix designations because I'm lazy. All you have to do is continue to fill in the "million", "billion", etc. array and the "tenths", "hundredths", etc. array.

Anyway, here's the code with some test cases at the bottom:
PHP Code:
``` //see the improved version below  ```
-Dan
Last edited by ManiacDan; January 5th, 2012 at 11:00 AM.
8. PHP Code:
``` <?php //new and improved recursive version of my last attempt: function print_number_as_words2(\$number) {     if ( !is_string(\$number) && !is_float(\$number) && !is_int(\$number) )     {         return false;     }     if ( is_string(\$number) )     {         //we know it's a string.  see if there's a negative:         if ( substr(\$number, 0, 1) == '-' )         {             \$number = substr(\$number, 1);             \$number = \$number * -1;         }     }     \$number = strval(\$number);     if ( \$number == '0' )     {         return "Zero";     }     \$negative = \$number < 0 ? "Negative" : "";     \$number = trim(\$number, '-');     \$split_by_decimal = explode(".", \$number);     \$decimal_string = '';     if ( count(\$split_by_decimal) > 1 )     {         \$decimal_string = process_decimal(\$split_by_decimal[1]);     }     return trim(preg_replace("#\s+#", " ", \$negative . " " . process_number(\$split_by_decimal[0]) . " " . \$decimal_string)); } function process_number(\$number, \$depth = 0) {     \$group_designators = array(         "",         "Thousand",         "Million",         "Billion",         "Trillion",         "Quadrillion",         "Quintillion",         "Sextillion",         "Septillion",         /*that's enough for now*/);     \$numbers = array(         '1'=>"One",         '2'=>"Two",         '3'=>"Three",         '4'=>"Four",         '5'=>"Five",         '6'=>"Six",         '7'=>"Seven",         '8'=>"Eight",         '9'=>"Nine",         '10'=>"Ten",         '11'=>"Eleven",         '12'=>"Twelve",         '13'=>"Thirteen",         '14'=>"Fourteen",         '15'=>"Fifteen",         '16'=>"Sixteen",         '17'=>"Seventeen",         '18'=>"Eighteen",         '19'=>"Nineteen",         '20'=>"Twenty",         '30'=>"Thirty",         '40'=>"Fourty",         '50'=>"Fifty",         '60'=>"Sixty",         '70'=>"Seventy",         '80'=>"Eighty",         '90'=>"Ninety",         );     //we already know that we have a numeric string.  Process it in groups of three characters     while ( strlen(\$number) > 0 )     {         if ( strlen(\$number) <= 3 )         {             \$number_to_process = \$number;             \$number = '';         }         else         {             \$number_to_process = substr(\$number, strlen(\$number) - 3);             \$number = substr(\$number, 0, strlen(\$number) - 3);         }         if ( strlen(\$number_to_process) == 3 )         {             \$output[] = \$numbers[substr(\$number_to_process, 0, 1)];             \$output[] = "Hundred";             \$number_to_process = substr(\$number_to_process, 1);         }         if ( isset(\$numbers[\$number_to_process]) )         {             \$output[] = \$numbers[\$number_to_process];         }         else         {             //we're dealing with a number greater than 20 and not divisible by 10:             \$tens = substr(\$number_to_process, 0, 1) . "0";             \$ones = substr(\$number_to_process, 1, 1);             \$output[] = \$numbers[\$tens];             \$output[] = \$numbers[\$ones];         }         return process_number(\$number, \$depth+1) . " " . implode(" ", \$output) . " " . \$group_designators[\$depth];     } } function process_decimal(\$number) {     \$suffix = array(         "Tenths",         "Hundreths",         "Thousandths",         "Ten Thousandths",         "Hundred Thousandths",         "Millionths",         //enough         );     return " and " . process_number(\$number) . \$suffix[strlen(\$number) - 1]; } echo print_number_as_words2("-19832498347.34") . "\n"; echo print_number_as_words2(14.12) . "\n"; echo print_number_as_words2(1432489723485) . "\n"; echo print_number_as_words2(10234.45645) . "\n"; echo print_number_as_words2(-10.2) . "\n"; echo print_number_as_words2("1298721498732.111111") . "\n"; ```
-Dan