[Solved] Converting numbers to words

I have the following script to convert invoice balance figures into words, which I have inserted into /applications/views/invoice_templates/pdf/default.php

<?php 
function numtowords($num){ 
$ones = 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" 
); 
$tens = array( 
2 => "twenty", 
3 => "thirty", 
4 => "forty", 
5 => "fifty", 
6 => "sixty", 
7 => "seventy", 
8 => "eighty", 
9 => "ninety" 
); 
$hundreds = array( 
"hundred", 
"thousand", 
"million", 
"billion", 
"trillion", 
"quadrillion" 
); //limit t quadrillion 
$num = number_format($num,2,".",","); 
$num_arr = explode(".",$num); 
$wholenum = $num_arr[0]; 
$decnum = $num_arr[1]; 
$whole_arr = array_reverse(explode(",",$wholenum)); 
krsort($whole_arr); 
$rettxt = ""; 
foreach($whole_arr as $key => $i){ 
if($i < 20){ 
$rettxt .= $ones[$i]; 
}elseif($i < 100){ 
$rettxt .= $tens[substr($i,0,1)]; 
$rettxt .= " ".$ones[substr($i,1,1)]; 
}else{ 
$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0]; 
$rettxt .= " ".$tens[substr($i,1,1)]; 
$rettxt .= " ".$ones[substr($i,2,1)]; 
} 
if($key > 0){ 
$rettxt .= " ".$hundreds[$key]." "; 
} 
} 
if($decnum > 0){ 
$rettxt .= " and "; 
if($decnum < 20){ 
$rettxt .= $ones[$decnum]; 
}elseif($decnum < 100){ 
$rettxt .= $tens[substr($decnum,0,1)]; 
$rettxt .= " ".$ones[substr($decnum,1,1)]; 
} 
} 
return $rettxt; 
} 
echo numtowords("30000000.00");
?>

This code works fine if the number is directly inserted into the line “echo numtowords…” line as shown above. But if I replace the number with the invoice balance variable to read as follows,

<?php echo numtowords($invoice->invoice_balance); ?>

I get two errors:

Undefined offset: 0
Undefined index: 000

What am I doing wrong?

Try a <?php print_r($invoice->invoice_balance) ?> and take a look at the output. It seems that $invoice->invoice_balance is an array or holds multiple values.

So, I would modify the code to be as follows?

<?php echo numtowords(print_r($invoice->invoice_balance)); ?>

The print_r line on its own returns the correct invoice balance decimal amount without the comma separators

No, just add <?php print_r($invoice->invoice_balance) ?> at the bottom of the page.

I put your code at the bottom of the page as instructed, but all I get is the numeric value of $invoice->invoice_balance

Hmm good question what the problem is. :confused:

Using the code in my 1st message, the line above converts the figure “30000000.00” to “THIRTY MILLION” if the actual nummbers are used in the numtowords function.

However, if I replace the figure in the above line with the invoice balance variable so that it reads as follows, I get the error messages.

echo numtowords($invoice->invoice_balance);

You expect a string in your functions, but invoice_balance is an integer.

echo numtowords((string) $invoice->invoice_balance);

should do the trick.

@Jadaw1n:: Thanks!! That did it for me!!