currency to string ???
Moderator: General Moderators
currency to string ???
anybody know how to convert the $100 to ONE HUNDRED?
try this function
Mark
Code: Select all
<?php
function get_triad($int) {
/* * Converts whole numbers from 1 to 999 to "english" * Inspired by Onslaught (aka 'Mad Genius') */
$ret_str = '';
$temp = (string)$int;
$length = strlen($temp);
$single = array(0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine');
$teens = array(1 => 'eleven', 2 => 'twelve', 3 => 'thirteen', 4 => 'fourteen',5 => 'fifteen', 6 => 'sixteen', 7 => 'seventeen', 8 => 'eighteen', 9 => 'nineteen');
$double = array(1 => 'ten', 2 => 'twenty', 3 => 'thirty', 4 => 'forty', 5 => 'fifty', 6 => 'sixty', 7 => 'seventy', 8 => 'eighty', 9 => 'ninety');
switch ($length) {
case 1: /*-- $int < 1,000 --*/
$ret_str .= $single[(int)$temp];
break;
case 2: /*-- tens / teens --*/
if ((int)$temp > 10 && (int)$temp < 20) {
$ret_str .= $teens[(int)substr($temp, 1)];
} else {
$ones = (int)substr($temp, 1) ? '-' . $single[(int)substr($temp, 1)] : null; $ret_str .= $double[(int)substr($temp, 0, 1)] . $ones;
}
break;
case 3: /*-- hundreds --*/
$ones = (int)substr($temp, 1);
$ones = ($ones > 0) ? ' ' . get_triad((int)substr($temp, 1)) . ' ' : ''; $ret_str .= $single[(int)substr($temp, 0, 1)] . ' hundred' . $ones;
break;
}
return $ret_str;
}
function num_to_string($int) {
if (!is_numeric($int)) {
echo "<br>\n<strong>Error :: </strong> num_to_string accepts numerical values only.<br>\n";
return null;
}
$multiple = array(0 => '', 1 => 'thousand', 2 => 'million', 3 => 'billion', 4 => 'trillion', 5 => 'quadrillion'); // and so on
/*-- split the number into groups of three --*/
$str = number_format(abs($int)); $str = explode(',',$str);
/*-- prevent array index errors if number too big --*/
$counter = count($str);
if ($counter > count($multiple)) {
echo "<br>\n<strong>Error :: </strong> Number too large.<br>\n";
return null;
}
/*-- assemble the output --*/
for ($i = 0; $i < count($str); $i++) {
$counter--; $str[$i] = get_triad((int)$str[$i]) . ' ' . $multiple[$counter];
}
/*-- include some commas as needed for readability --*/
$str = ($int > 9999) ? implode($str,', ') : implode($str,' ');
/*-- account for negative numbers --*/
$str = ($int < 0) ? 'negative ' . $str : $str;
return $str;
}
echo num_to_string(-7) . "<br>\n"; // 'negative seven'
echo num_to_string(140) . "<br>\n"; // 'one hundred forty'
echo num_to_string(2300) . "<br>\n"; // 'two thousand three hundred '
echo num_to_string(123456789) . "<br>\n"; // 'one hundred twenty-three million, four hundred fifty-six thousand, // seven hundred eighty-nine '
echo num_to_string(1234567898765432) . "<br>\n"; // 'one quadrillion, two hundred thirty-four trillion, // five hundred sixty-seven billion, eight hundred ninety-eight million, // seven hundred sixty-five thousand, four hundred thirty-two // // Numbers above a quadrillion or so get a bit dodgy...
?>Thank you 4 reply
one of the function i found
http://www.phpfreaks.com/quickcode/Nume ... ng/167.php
one of the function i found
http://www.phpfreaks.com/quickcode/Nume ... ng/167.php