in reply to Rounding a number using (s)printf

It don't think it has anything to do with how the numbers are represented in floating point. I think what you're seeing is a result of sprintf() following IEEE rounding semantics. It's something like even least significant digits round up at the half-way mark and odd round down at the half-way mark.

Search google for IEEE rounding semantics and I'm sure you'll find a better reference than I.

Update: doh! There's even a mention in perldoc -q round

Replies are listed 'Best First'.
Re^2: Rounding a number using (s)printf
by Anonymous Monk on Jan 20, 2005 at 09:55 UTC
    You mean bankers rounding? That's not what's happening here. As you said yourself, the least significant digits play a role here. But the difference between 0.255 and 1.255 lies is the most significant digit.
Re^2: Rounding a number using (s)printf
by ysth (Canon) on Jan 20, 2005 at 22:45 UTC
    Did you try it? It has everything to do with how numbers are represented in floating point. There are numbers that can be exactly represented in floating point where the rounding is ambiguous, e.g. printf "%.1f", $_ for .25, .75, and a round-to-even-last-digit rule could apply, but in fact perl relies on C's printf(), and the SUSv3 standard says only "The low-order digit shall be rounded in an implementation-defined manner." Depending on how you interpret "rounded" in that sentence, you may not even be able to count on it rounding .24 to ".2".

    In point of fact, I don't know of any system that fails to round at all, but do know of some that do not consistently either apply or fail to apply a round-to-even-last-digit rule.