note 6262 modified in printf by ronabop
| From: | ronabop@php.net | Date: | Fri, 11 Aug 2000 18:01:56 +0000 |
| Subject: | note 6262 modified in printf by ronabop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-1357@lists.php.net to get a copy of this message | ||
Someone mentioned that the dropping of digits is pernicious; here's a small function you can
throw in an include file or something to ensure some accurate precision.
//---------------------
// float precision (float, integer)
//
// - jlph 6.30.2000
//
// A function to limit the # of decimal places
// displayed in a float. Takes a float and an integer.
// The float will be taken to the decimal place
// specified by the integer and rounded up accordingly.
// Returns a float with the corners smoothed off.
//---------------------
function precision ($value, $place){
$value = $value * pow(10, $place + 1);
$value = floor($value);
$value = (float) $value/10;
(float) $modSquad = ($value - floor($value));
$value = floor($value);
if ($modSquad > .5){
$value++;
}
return $value / (pow(10, $place));
}
//---------------------
http://www.php.net/manual/function.printf.php