note 6262 modified in function.printf by felipe
| From: | felipe@php.net | Date: | Sun, 20 Jul 2008 23:47:27 +0000 |
| Subject: | note 6262 modified in function.printf by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142366@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.
<?php
//---------------------
// 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));
}
?>
--was--
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://php.net/manual/en/function.printf.php