Number to wording Conversion

function convertNumberToWord($num = false)
{
$num = str_replace(array(‘,’, ‘ ‘), ” , trim($num));
if(! $num) {
return false;
}
$num = (int) $num;
$words = array();
$list1 = array(”, ‘one’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’, ‘seven’, ‘eight’, ‘nine’, ‘ten’, ‘eleven’,
‘twelve’, ‘thirteen’, ‘fourteen’, ‘fifteen’, ‘sixteen’, ‘seventeen’, ‘eighteen’, ‘nineteen’
);
$list2 = array(”, ‘ten’, ‘twenty’, ‘thirty’, ‘forty’, ‘fifty’, ‘sixty’, ‘seventy’, ‘eighty’, ‘ninety’, ‘hundred’);
$list3 = array(”, ‘thousand’, ‘million’, ‘billion’, ‘trillion’, ‘quadrillion’, ‘quintillion’, ‘sextillion’, ‘septillion’,
‘octillion’, ‘nonillion’, ‘decillion’, ‘undecillion’, ‘duodecillion’, ‘tredecillion’, ‘quattuordecillion’,
‘quindecillion’, ‘sexdecillion’, ‘septendecillion’, ‘octodecillion’, ‘novemdecillion’, ‘vigintillion’
);
$num_length = strlen($num);
$levels = (int) (($num_length + 2) / 3);
$max_length = $levels * 3;
$num = substr(’00’ . $num, -$max_length);
$num_levels = str_split($num, 3);
for ($i = 0; $i < count($num_levels); $i++) {
$levels–;
$hundreds = (int) ($num_levels[$i] / 100);
$hundreds = ($hundreds ? ‘ ‘ . $list1[$hundreds] . ‘ hundred’ . ( $hundreds == 1 ? ” : ‘s’ ) . ‘ ‘ : ”);
$tens = (int) ($num_levels[$i] % 100);
$singles = ”;
if ( $tens < 20 ) {
$tens = ($tens ? ‘ ‘ . $list1[$tens] . ‘ ‘ : ” );
} else {
$tens = (int)($tens / 10);
$tens = ‘ ‘ . $list2[$tens] . ‘ ‘;
$singles = (int) ($num_levels[$i] % 10);
$singles = ‘ ‘ . $list1[$singles] . ‘ ‘;
}
$words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ‘ ‘ . $list3[$levels] . ‘ ‘ : ” );
} //end for loop
$commas = count($words);
if ($commas > 1) {
$commas = $commas – 1;
}
return implode(‘ ‘, $words);
}

$amount = 25000.95;

$get_cents = explode(“.”,$amount);

$cents = $get_cents[1];
if($cents==””)
{
$cents= “00”;
}
$final_format = convertNumberToWord($amount).”and $cents/100*****”;
$final_format = strtoupper($final_format);
echo $final_format;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s