note 25489 added to function.round

From: Date: Wed, 25 Sep 2002 20:42:59 +0000
Subject: note 25489 added to function.round
Groups: php.notes 
Request: Send a blank email to php-notes+get-37241@lists.php.net to get a copy of this message
Here is another custom function to format US currency. It rounds to the second decimal place, but NOT by using a float rounding function, so it will always return the correct answer. It just checks the third decimal place to see if it's greater than 4. If it is, up you go. If it's not, no change. This is all the rounding functionality you need for currency. The function also can add commas if you set the comma flag. function FormatCurrency($amount, $commas=NULL) { // Formats any number into valid US currency // Inserts commas if $commas flag is set // Output should match regular expression ^-?([0-9]+|[0-9]{1,3}(,[0-9]{3})*)\.[0-9]{2}$ $amount = str_replace(",", "", $amount); // Strip out commas if ($amount == 0) return "0.00"; // Catch zero values if ($amount < 0) { // If we are dealing with a negative number... $amount = substr($amount, 1); // Cut off the minus sign $negative = "-"; // Set the $negative flag } $amount = explode(".", $amount); // Explode $amount into an array $integer = $amount[0]; // Rename left of decimal $integer $decimal = $amount[1]; //Rename right of decimal $decimal if ($integer != 0) { // If there is an integer... $zeros = strspn($integer, "0"); // Find # of leading zeros in $integer $integer = substr($integer, $zeros); // Strip leading zeros from $integer } else $integer = 0; // Else use a zero as a placeholder if ($decimal) { // If there is a decimal... if (substr($decimal, 2, 1) > 4) { // If we need to round up... $decimal = substr($decimal, 0, 2); // Truncate to 2 decimal places if ($decimal == 99) { // If the decimal is 99... $decimal = 00; // Set the decimal to 00 $integer++; // Increment the integer } elseif ($decimal < 9) { // Catch decimals with leading zeros... $decimal++; // Increment the decimal $decimal = 0 . $decimal; // And manually replace the zero } else $decimal++; // Else just increment the decimal } else $decimal = substr($decimal, 0, 2); // We still need to truncate to 2 decimal places if (strlen($decimal) == 1) $decimal .= "0"; // If length is 1, add a zero } else $decimal = "00"; // Else set the decimal to 00 $length = strlen($integer); // Store the length of the integer in $length if ($commas && $length > 3) { // If we need to add commas... for ($i=3; $i<$length; $i=$i+3) { // Loop $length / 3 times $integer = substr($integer, 0, $length - $i) . "," . substr($integer, $length - $i); // Split $integer at $length - $i and rejoin around a comma } } $amount = $negative . $integer . "." . $decimal; // Rebuild the $amount return $amount; // Return the formatted $amount } Please comment with ways to shorten (that don't obscure or change the functionality), and any bugs you find. -- http://www.php.net/manual/en/function.round.php http://master.php.net/manage/user-notes.php?action=edit+25489 http://master.php.net/manage/user-notes.php?action=delete+25489 http://master.php.net/manage/user-notes.php?action=reject+25489

« previous php.notes (#37241) next »