in reply to Rounding a number using (s)printf

This is a mathematical rule of rounding of numbers: if digit before last visible digit is greater than 5 last digit is increased by 1, otherwise the digit saves its value.

So, 0.256 ~ 0.26, 0.255 ~ 0.25, 0.254 ~ 0.25. This is correct results.

---
Michael Stepanov aka nite_man

It's only my opinion and it doesn't have pretensions of absoluteness!

Replies are listed 'Best First'.
Re^2: Rounding a number using (s)printf
by Anonymous Monk on Jan 20, 2005 at 10:02 UTC
    Could you please first to read the entire question before babbling your response?

    First of all, you're wrong. The OP expected the results for 0.256, 0.255 and 0.254 to be 0.26, 0.26 and 0.25. And lo and behold, they were 0.26, 0.26 and 0.25. Because everyone learns in primary school that if the part we truncate starts with 5 or greater (and not "greater than 5"), we up the last digit.

    But the OPs problem wasn't with that. The OPs problem was the inconsistency of 0.255 being rounded up, and 1.255 being rounded down.

    That, however, you don't address. Others in the thread luckely did.