# money to text

By martin dempsey
on August 8, 2003

Version: 1.1

Type: Function

Category: Money

Description: This function converts a number (1354.20) into a string (one thousand three hundred fifty four dollars and twenty cents).

usage:
\$var=1354.20;
echo numtotext(\$var);

```<?php
// works upto 100 Billion Dollars less one cent.
// returned text is always positive
// Version 1.1
// useage: numtotext(somenumber) -> returns a text string

function numtotext(\$num)
{
\$tdiv = array("cents ","dollars and ","hundred ","thousand ", "hundred ",
"million ", "hundred ","billion ");
\$divs = array( 0,0,0,0,0,0,0);
\$pos = 0; // index into tdiv;
//make num a string, and reverse it, because we run through it backwards
\$num=strval(strrev(number_format(\$num,2,'.','')));
\$answer = ""; // build it up from here
while (strlen(\$num)) {
if ( strlen(\$num) == 1 || (\$pos >2 && \$pos % 2 == 1))  {
\$num= substr(\$num,1);
}
else {
\$num= substr(\$num,2);
if (\$pos < 2)
\$pos++;
}
if (substr(\$num,0,1) == '.') {
\$num= substr(\$num,1);
// put in "zero" dollars if there are none
if (strlen(\$num) == 1 && \$num == '0') {
\$num= substr(\$num,1);
}
}
if (\$pos >= 2 && strlen(\$num)) {
if (substr(\$num,0,1) != 0  || (strlen(\$num) >1 && substr(\$num,1,1) != 0
&& \$pos %2 == 1)  ) {
// check for missed millions and thousands when doing hundreds
if ( \$pos == 4 || \$pos == 6 ) {
if (\$divs[\$pos -1] == 0)
}
// standard
\$divs[\$pos] = 1;
}
else {
\$pos++;
}
}
}
}

function doone(\$onestr)
{
\$tsingle = array("","one ","two ","three ","four ","five ",
"six ","seven ","eight ","nine ");

}

function dotwo(\$twostr)
{
\$tdouble = array("","ten ","twenty ","thirty ","fourty ","fifty ",
"sixty ","seventy ","eighty ","ninety ");
\$teen = array("ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ",
"sixteen ","seventeen ","eighteen ","nineteen ");

if ( substr(\$twostr,1,1) == '0') {
\$ret = doone(substr(\$twostr,0,1));
}
else if (substr(\$twostr,1,1) == '1') {
\$ret = \$teen[substr(\$twostr,0,1)];
}
else {
\$ret = \$tdouble[substr(\$twostr,1,1)] . doone(substr(\$twostr,0,1));
}
return \$ret;
}
?>
```
﻿