Skip to content

Instantly share code, notes, and snippets.

@spencejs
Created March 28, 2013 22:40
Show Gist options
  • Save spencejs/5267429 to your computer and use it in GitHub Desktop.
Save spencejs/5267429 to your computer and use it in GitHub Desktop.
Dollarfy and Commafy Functions - Renders numbers in money and comma formats (Commafy can be used alone, but Dollarfy requires Commafy) Use: echo dollarfy($somenumber);
//Dollarfy Function
function dollarfy ($num,$dec="2") {
$format="%.$dec" . "f";
$number=sprintf($format,$num);
$str=strtok($number,".");
$dc=strtok(".");
$str=commafy($str);
$return="\$$str";
if ($dec!=0){
$return = "$return" . ".$dc";
}
return($return);
}
//Commafy Function
function commafy ($str) {
$n = strlen($str);
if ($n <= 3) {
$return=$str;
}
else {
$pre=substr($str,0,$n-3);
$post=substr($str,$n-3,3);
$pre=commify($pre);
$return="$pre,$post";
}
return($return);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment