[Pugged] Whoohoo!!

Dave J. Hala Jr. ciapug@ciapug.org
01 Sep 2002 15:14:17 -0500


Here it is.... Ok, so this is a first.... First time I've ever posted
any code as an actual "real" programmer.  Be gentle!

All you need to do is send a number to numto_word and it will return the
text.

<?
function numto_word($number)
{
#
# set all variables to "";
#
$tnum =  "";$tens = "";$teens = "";$hun = "";$thou = "";$tenthou =
"";$hunthou = "";
$converted = "";

switch($number)
{
case  ($number < 10):
	$fchar = substr($number,-1,1);
	$ones = ones($fchar);
	$converted = "$hunthou$tenthou$teenthou$thou$hun$tens$teens$ones";
	$converted .= " Dollars";
	#echo "Converted: $number $converted<BR>";
	break;

case ($number < 100):
	# position for ones
	$fchar = substr($number,-1,1);
	# postion for tens
	$schar = substr($number,-2,1);
	# position for hundreds
	# position for ten thousands
	# this makes the exception for teens and ten
	if ($schar == "1"){$teens = teens($fchar); $fchar = ""; }

	$ones = ones($fchar);
        $tens = tenzs($schar);
	$converted = "$tens$teens$ones";
	$converted .= " Dollars";
	#echo "Converted: $number $converted<BR>";
	break;

case ($number < 1000):
	# position for ones
	$fchar = substr($number,-1,1);
	# postion for tens
	$schar = substr($number,-2,1);
	# position for hundreds
	$hchar = substr($number,-3,1);
	# this makes the exception for teens and ten
	if ($schar == "1"){$teens = teens($fchar); $fchar = ""; }

	$ones = ones($fchar);
	$tens = tenzs($schar);
	$hun = hundreds($hchar);

	$converted = "$hun$tens$teens$ones";
	$converted .= " Dollars";
	#echo "Converted: $number $converted<BR>";
	break;

case ($number < 10000):
	# position for ones
	$fchar = substr($number,-1,1);
	# postion for tens
	$schar = substr($number,-2,1);
	# position for hundreds
	$hchar = substr($number,-3,1);
	# position for thousands
	$tchar = substr($number, -4,1);
	# position for ten thousands
	# this makes the exception for teens and ten
	if ($schar == "1"){$teens = teens($fchar); $fchar = ""; }

	$ones = ones($fchar);
	$tens = tenzs($schar);
	$hun = hundreds($hchar);
	$thou = thousands($tchar);

	$converted = "$thou$hun$tens$teens$ones";
	$converted .= " Dollars";
	#echo "Converted: $number $converted<BR>";
	break;

case ($number < 100000):
	# position for ones
	$fchar = substr($number,-1,1);
	# postion for tens
	$schar = substr($number,-2,1);
	# position for hundreds
	$hchar = substr($number,-3,1);
	# position for thousands
	$tchar = substr($number, -4,1);
	# position for ten thousands
	$tentchar = substr($number, -5,1);
	# this makes the exception for teens and ten
	if ($schar == "1"){$teens = teens($fchar); $fchar = ""; }
	# this makes the exception for teen thousands
	if ($tentchar == "1"){$teenthou = tnthous($tchar); $tchar = "";}

	$ones = ones($fchar);
	$tens = tenzs($schar);
	$hun = hundreds($hchar);
	$thou = thousands($tchar);
	$tenthou = tenthousands($tentchar);

	$converted = "$tenthou$teenthou$thou$hun$tens$teens$ones";
	$converted .= " Dollars";
	#echo "Converted: $number $converted<BR>";
	break;

case ($number < 1000000):
	# position for ones
	$fchar = substr($number,-1,1);
	# postion for tens
	$schar = substr($number,-2,1);
	# position for hundreds
	$hchar = substr($number,-3,1);
	# position for thousands
	$tchar = substr($number, -4,1);
	# position for ten thousands
	$tentchar = substr($number, -5,1);
	# position for hundred thousands
	$hunthou = substr($number, -6,1);
	# this makes the exception for teens and ten
	if ($schar == "1"){$teens = teens($fchar); $fchar = ""; }
	# this makes the exception for teen thousands
	if ($tentchar == "1"){$teenthou = tnthous($tchar); $tchar = "";}
	# put an exception her for teen millions...
	$ones = ones($fchar);
	$tens = tenzs($schar);
	$hun = hundreds($hchar);
	$thou = thousands($tchar);
	$tenthou = tenthousands($tentchar);
	$hunthou = hundredthou($hunthou);

	$converted = "$hunthou$tenthou$teenthou$thou$hun$tens$teens$ones";
	$converted .= " Dollars";
	#echo "Converted: $number $converted<BR>";
	break;
case ($number < 10000000):
	# this check will be for more than $999,999
	# which is a bad thing...
	$converted = "*VOID*VOID*VOID*VOID*VOID*VOID*VOID*VOID*VOID*";
	#echo "Converted: $number $converted<BR>";
	break;

}
return($converted);

}


function ones($ones)
{
$word = "";
$onze = array("","one", "two", "three", "four","five", "six", "seven",
"eight","nine");
$word = $onze[$ones];
return($word);

}

function teens($teen)
{
#echo "FC: TEEN $teen <BR>";
$word = "";
$teens = array("ten", "eleven", "twelve", "thirteen","fourteen",
"fifteen", "sixteen", "seventeen","eighteen","nineteen");
$word = $teens[$teen];
return($word);

}

function tenzs($ten)
{
$word = "";
$tens = array("","", "twenty ", "thirty ", "forty ","fifty ", "sixty ",
"seventy ", "eighty ","ninety ");
$word = $tens[$ten];
return($word);
}

function hundreds($hun)
{
$word = "";
$teens = array("","one hundred ", "two hundred ", "three hundred ",
"four hundred ","five hundred ", "six hundred ", "seven hundred ",
"eight hundred ","nine hundred ");
$word = $teens[$hun];
return($word);
}

function thousands($thou)
{
$word = "";
$teens = array("","one thousand ", "two thousand ", "three thousand ",
"four thousand ","five thousand ", "six thousand ", "seven thousand ",
"eight thousand ","nine thousand ");
$word = $teens[$thou];
return($word);
}

function tnthous($teen)
{
#echo "FC: TEEN $teen <BR>";
$word = "";
$teens = array("ten thousand", "eleven thousand", "twelve thousand",
"thirteen thousand","fourteen thousand", "fifteen thousand", "sixteen
thousand", "seventeen thousand ","eighteen thousand","nineteen
thousand");
$word = $teens[$teen];
return($word);

}

function tenthousands($tnth)
{
#echo "FC: TEEN $teen <BR>";
$word = "";
$tens = array("","", "twenty ", "thirty ", "forty ","fifty ", "sixty ",
"seventy ", "eighty ","ninety ");
$word = $tens[$tnth];
return($word);

}

function hundredthou($hunthou)
{
$word = "";
$teens = array("","one hundred ", "two hundred ", "three hundred ",
"four hundred ","five hundred ", "six hundred ", "seven hundred ",
"eight hundred ","nine hundred ");
$word = $teens[$hunthou];
return($word);
}


On Sun, 2002-09-01 at 13:38, Travis Carden wrote:
> Cool.  Let's see the code!  =)
> 
> Travis Carden, Web Developer
> http://www.traviscarden.com/
>  
>  
> 
> 
> -----Original Message-----
> From: ciapug-admin@ciapug.org [mailto:ciapug-admin@ciapug.org] On Behalf
> Of Dave J. Hala Jr.
> Sent: Sunday, September 01, 2002 11:38 AM
> To: PHP List
> Subject: [Pugged] Whoohoo!!
> 
> 
> I  finally got the PHP code that converts 1,256 to "one thousand two
> hundred and fifty six" working.   
> 
> I used Dave's case statement and the array idea. I'll post the code
> (even though its a little rough) If anyone is interested in it...
> 
> BTW, Dave -thanks for the comment about the differance between
> "technically perfect" code and "commercial" code.  It really helped. 
> After your comment,  I realized that sometimes I just have to make
> things work enough though the code doesn't always do it the best
> way...   Now my project is moving forward a little faster...
> 
> thanks again..
> 
> 
> -- 
> OSIS
> Dave J. Hala Jr.
> 641.475.1606
> 
> _______________________________________________
> Ciapug mailing list
> Ciapug@ciapug.org
> http://cialug.org/mailman/listinfo/ciapug
> 
> 
> 
> _______________________________________________
> Ciapug mailing list
> Ciapug@ciapug.org
> http://cialug.org/mailman/listinfo/ciapug
-- 
OSIS
Dave J. Hala Jr.
641.475.1606