Re: Beware of round() function
| From: | Kirk dot Johnson at zootweb dot com | Date: | Tue, 25 Mar 2008 16:55:27 +0000 |
| Subject: | Re: Beware of round() function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-272084@lists.php.net to get a copy of this message | ||
Thanks for the info, Jeremy. Regardless of the technical details, my code
still broke. I am little discouraged that an operation that should be so
simple has these sorts of gotchas.
BTW, I ended up casting to int as my solution.
Kirk
Jeremy Privett <jeremy@omegavortex.net> wrote on 03/24/2008 02:04:48 PM:
> Jeremy Privett wrote:
> > Kirk.Johnson@zootweb.com wrote:
> >> Beware: round() apparently has changed its behavior from PHP 4.
> >
> > This is actually a change in the behavior of the float type, not the
> > round function. Replace your round() with a cast to float and you'll
> > see the exact same result.
> >
>
> Also, as a side-note, the only way I've found to get these numbers to
> print properly is through either printf or sprintf. Also, you could cast
> back to an integer, if you explicitly don't need floats.