Re: round()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxxx <admin@xxxxxxxxxxxxxxxxxxx> 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.
>
>
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux