Created
March 28, 2013 22:40
-
-
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);
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
//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