Re: round()
| From: | Bob Chatman | Date: | Wed, 10 Oct 2007 23:47:19 +0000 |
| Subject: | Re: round() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-263074@lists.php.net to get a copy of this message | ||
Why arent you just using sprintf?
$var = 5.555;
printf("%.2f", $var); # output : 5.56
printf("%.1f", $var); # output : 5.6
printf("%.0f", $var); # output : 6
On 10/10/07, admin@buskirkgraphics.com <admin@buskirkgraphics.com> wrote:
>
> > While we're entertaining algorithms, has anyone else noticed that
>
> > php's round() isn't the most accurate algorithm to round?
>
>
>
> If you will refer to chafy's reply on 28-Feb-2007 06:13
> <http://us2.php.net/manual/en/function.round.php#73537>
>
> The function round numbers to a given precision.
>
> function toFixed($number, $round=2)
> {
>
> $tempd = $number*pow(10,$round);
> $tempd1 = round($tempd);
> $number = $tempd1/pow(10,$round);
> return $number;
>
> }
> echo toFixed(5.555,2); //return 5.56
>
>
>
> If your rounding issue is passed 2 decimal places. I found this function
> covenant and problem solving. As for the accuracy of the algorithm
>
> I disagree. I will need to see an example where the round() is inaccurate.
>
>
>
>