# Re: [PHP] numeric to word function

```Hi,

Friday, September 13, 2002, 12:37:41 AM, you wrote:
rhc> Hi..
rhc> anyone know any function to convert numeric values to word..
rhc> something like this:
1252 -->> one thousand two hundred and fifty two```
```
rhc> Thanks...
Here is a class that will get you started :)

class num_to_text {
var \$words =
array(0=>array('zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty'),

1=>array('zero','one','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'),

2=>array('thousand','million'));
function num_class(\$mode = 'NUMERIC'){
\$this_.mode = \$mode;
//this bit to do
}
function convert(\$num){
return \$this->num(\$num,1);
}
function num(\$num,\$key = 0){
static \$t;
static \$call;
\$res = intval(\$num/100);
\$left = \$num%100;
if(\$key == 1){ //first non recursive call reset text and
thousand/million toggle
\$t = "";
\$call = 0;
}
else{
// do we need to print thousand as this is a recursive call
if(\$num%1000 > 0)\$t = ' '.\$this->words[2][\$call].' '.\$t;
\$call ^= 1;     //toggle call for next recursion
}
if(\$left <= 20){ // no need to split it
if(\$left == 0){ // is the whole mess just zero
if(\$res == 0) \$t = \$this->words[0][\$left].\$t;
}
else{
\$t = \$this->words[0][\$left].\$t;
}
}
else{ //need to split it up
\$tens = intval(\$left/10);
\$units = \$left%10;
(\$units > 0)? \$units = ' '.\$this->words[0][\$units] : \$units =
''; // eg thirty or thirty one
\$t = \$this->words[1][\$tens].\$units.\$t;
}
if(\$res < 10){
if(\$res > 0){
(\$left == 0)? \$and = '':\$and = ' and '; // do we need
to print 'and'
if(\$res > 0) \$t = \$this->words[0][\$res]. "
hundred".\$and.\$t;
}
}
else{
\$res = \$res%10;
if(\$res > 0){
(\$left == 0)? \$and = '':\$and = ' and ';
\$t = \$this->words[0][\$res]. " hundred".\$and.\$t;
}
else{
if(\$left > 0)\$t = ' and '.\$t;
}
\$num = intval(\$num/1000);
num(\$num);
}
return \$t;
}
}

//usage

\$n2t = new num_to_text();
for(\$num = 0;\$num < 1102;\$num++){
echo \$num.' '.\$n2t->convert(\$num).'<br>';
}

--
regards,
Tom

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

```