Converting numbers to words

Im trying to convert numbers to words (without using any functions, the range is between -1000 to 1000, the code should be able to print the numbers in words, but Im stuck and dont know where to go from here, can you please assist me, below is the code so far

[php]<?php

$units = array(“zero”, “one”, “two”, “three”, “four”, “five”,
“six”, “seven”, “eight”, “nine”, “ten”, “eleven”,
“twelve”, “thirteen”, “forteen”, “fifteen”,
“sixteen”, “seventeen”, “eighteen”, “nineteen”);

$tens = array(“twenty”, “thirty”, “forty”, “fifty”, “sixty”,
“seventy”, “eighty”, “ninety”);

$hundreds = array(“hundred”);
$thousand = array(“thousand”);

$number = -11;
$signedMinus = "Minus ";
$output = “”;
$n = ($number%10); // ones
$tn = ($number/10); // tens
$hn = ($number/100); // hundreds
$thn = ($number/1000); // thousands
$convertor = “”;

if ($number<0) {
$output .=$signedMinus;
$number = abs($number);
}
else
{
$output .="";
}
if (is_numeric($number)) { // error trapping
if ($number <=1000) { // within range
$output .=$units[$number];
}

}  

echo $output;

?>[/php]

you are using the offsets of the arrays as your numbers
so your arrays should be put in to place like below.

So now your numbers = the offset which = the word like they do with the units.
I hope this helps you out a little more.

[php]
$tens = array(20 => “twenty”, 30 =>“thirty”, 40 =>“forty”, 50 =>“fifty”, 60 =>“sixty”, 70 =>“seventy”, 80 =>“eighty”, 90 =>“ninety”);
$hundreds = array(100 =>“hundred”);
$thousand = array(1000 =>“thousand”);
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service