note 24271 added to language.types.float
| From: | jemima at london dot com | Date: | Sun, 11 Aug 2002 17:54:13 +0000 |
| Subject: | note 24271 added to language.types.float | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-34814@lists.php.net to get a copy of this message | ||
I found the following function useful. I'd obviously be interested to know if I've missed
something obvious.
function div($a, $b) {
// we can't divide 0, or divide by 0
if ($a == "0" || $b == "0") {
// return an error
return "Sorry, the input is not valid.";
}
// divide a by b, more accurately than php does otherwise
return ($a * $b) / ($b * $b);
}
e.g.
$test_num = '19.95';
echo 'The original number: ' . $test_num;
echo 'Div by 10 and use number_format: ' .
number_format($test_num / 10,"2");
// equals 1.99, not 2
echo 'As above but with the new function: ' .
number_format(div("$test_num", "10"),"2");
// now we get 2
--
http://www.php.net/manual/en/language.types.float.php
http://master.php.net/manage/user-notes.php?action=edit+24271
http://master.php.net/manage/user-notes.php?action=delete+24271
http://master.php.net/manage/user-notes.php?action=reject+24271