RE: [PHP-WIN] Rounding numbers with respect to percentages

From: Date: Mon, 28 Apr 2003 18:18:48 +0000
Subject: RE: [PHP-WIN] Rounding numbers with respect to percentages
Groups: php.windows 
Request: Send a blank email to php-windows+get-19693@lists.php.net to get a copy of this message
-----Original Message----- From: Herhuth, Ron To: php-windows@lists.php.net Sent: 2003-04-28 19:52 Subject: [PHP-WIN] Rounding numbers with respect to percentages >I am trying to dynamically calculate percentages and return the results >rounded off to the first decimal place. I tried using the >"round($num,1) " -- but the results don't always add up to 100% Pure luck will only give you 100% "fit". >"intval($num)" -- doesn't account for percentages that are less than 1 You might like to mulitiply with a factor given on how many decimals you need, and store the result as a float.... >"floatval($num) -- doesn't allow me to round off to a specificed decimal >place, which in return gets me back to the same problem as "round" floats are always tricky to deal with as money. Some casting might help you (I think one can cast in PHP, or?): $number_with_to_decimals = flotval( ((int)($num*100)) / 100 ) >How can I round off to the first decimal place making sure they add up >to 100%? You can't unless you applies some information saving algorithms. Rounding is an irreversible information lose, and can'ät be restored. You can only be very close. A trix is always to calculated with and store internaly with max presision, and only display numbers as ronded off. This is the only way to preserve the 100%. >Thanks, >Ron Nejma problema.

« previous php.windows (#19693) next »