RE: [PHP] How do I get the result of a Division to 2 decimal places?
| From: | Jay Blanchard | Date: | Tue, 09 Sep 2003 14:33:41 +0000 |
| Subject: | RE: [PHP] How do I get the result of a Division to 2 decimal places? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-162065@lists.php.net to get a copy of this message | ||
[snip]
I am dividing a number by another number and printing the result to the
screen. How can I ensure that the number is rounded up to 2 decimal
places?
[/snip]
Why others never test these things I'll never know...but here is a small
test you can run....
<?php
/*
** rounding / number format test
*/
$number = 12.2345;
echo number_format($number, 4, '', ',')."\n";
echo number_format($number, 3, '', ',')."\n";
echo number_format($number, 2, '', ',')."\n";
echo "\n";
echo round($number, 4)."\n";
echo round($number, 3)."\n";
echo round($number, 2)."\n";
?>
The results ....
12.2345
12.235
12.23
12.2345
12.235
12.23
Interesting, no? It would appear that under PHP4.2.1 on a test server
that number_format() exhibits the same behaviour as round().
Have a pleasant and productive day.