Last active
October 19, 2016 11:42
-
-
Save bwente/10e42b539de4ac203674 to your computer and use it in GitHub Desktop.
Output an easy-to-read number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$optionsXpld = @explode('&', $options); | |
$optionsArray = array(); | |
foreach ($optionsXpld as $xpld) { | |
$params = @explode('=', $xpld); | |
array_walk($params, create_function('&$v', '$v = trim($v);')); | |
if (isset($params[1])) { | |
$optionsArray[$params[0]] = $params[1]; | |
} else { | |
$optionsArray[$params[0]] = ''; | |
} | |
} | |
$stripWords = isset($optionsArray['stripWords']) ? $optionsArray['stripWords'] : null; | |
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : 0; | |
# Output easy-to-read numbers | |
# by james at bandit.co.nz | |
$number = $input; | |
// first strip any formatting; | |
$n = (0+str_replace(",","",$number)); | |
// is this a number? | |
if(!is_numeric($n)) return false; | |
// now filter it; | |
if ($n>1000000000000): | |
$num = round(($n/1000000000000),1); | |
$wrd = 'trillion'; | |
elseif ($n>1000000000): | |
$num = round(($n/1000000000),1); | |
$wrd = 'billion'; | |
elseif ($n>1000000): | |
$num = round(($n/1000000),1); | |
$wrd = 'million'; | |
elseif ($n>1000): | |
$num = round(($n/1000),1); | |
$wrd = 'thousand'; | |
elseif ($n>100): | |
$num = round(($n/100),1); | |
$wrd = 'hundred'; | |
endif; | |
if($stripWords=1){ | |
$output = number_format($num, $decimals); | |
}else{ | |
$output = number_format($num, $decimals) . " " . $wrd; | |
} | |
return $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment