Are all rounds created equal

  • Comments posted to this topic are about the item Are all rounds created equal

  • This is an excellent question, kinda reminded me of good ol' days when I worked with Access :-), thank you Timothy. The difference between Access and SQL Server is caused by the difference in so called mid point rounding rule:

    Access uses to even rule

    SQL Server uses away from zero rule.

    Oleg

  • Though I got it right, its nice to see difference between Access & SQL server in rounding the number. Thanks for the question, I remembered my old days when we worked in Access 🙂

    Thanks

  • Thanks for a good question!

    Here in the Netherlands, only one type of rounding is taught in school. The type that Wikipedia calls "away from zero" - you only check the first digit to be discarded, for 0-4 (truncate), or 5-9 (add one to previous digit).

    I was aware of "bankers rounding" (or "round to even") through questions in forums and usenet. But until I followed the Wikipedia link in the documentation, I did not klnow that there are so many other ways of rounding.

    Thanks!

    (BTW, did you also file a documentation bug because the SQL Server documentation doesn't specify its tie-breaking rule?)


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • My opinion is that none of the answers is correct.

    SQL Server will return 2.50 and 3.60, not 2.5 and 3.6.

  • Mighty (10/13/2010)


    My opinion is that none of the answers is correct.

    SQL Server will return 2.50 and 3.60, not 2.5 and 3.6.

    Wrong!

    SQL Server will return a result set that consists of two columns (both datatype numeric(3,2)), and one row. The values in this row are 2.5 and 3.6. (Or 2.50 and 23.60, or 0002.500000 and 03.6000 - those are all different representations of the same numeric value).

    It is your client software that converts this to a character representation so that you can see it on your screen. The client software chooses to represent these values with one trailing zero.

    (To check this, open an access project, create a pass-through query to execute this query on SQL Server, and heck the results. They'll show as 2.5 and 3.6. Or in my case as 2,5 and 3,6, since I have a Dutch version of Access, and the Dutch, like many other Europeans, use a decimal comma rather than a decimal point)


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • I had a suspicion the answer would be different, so I looked up the Access ROUND() function--didn't realise it did round-to-even before! At school I'm pretty sure we were always taught to round up (e.g. rounding -2.45 to 1 decimal place would become -2.4), though. (That was the best part of 30 years ago and my memory may be faulty on that point, though!).

  • Wrong!

    SQL Server will return a result set that consists of two columns (both datatype numeric(3,2)), and one row. The values in this row are 2.5 and 3.6. (Or 2.50 and 23.60, or 0002.500000 and 03.6000 - those are all different representations of the same numeric value).

    I don't agree with what you say.

    For me it's not about what is shown on the screen, but what is returned. SQL Server returns a decimal(3, 2), as you stated. So it will not store 2.5 or 2.500, which have a different precision, but exactly 2.50.

  • I got this right because we've been bitten by the same thing with VB6, which also uses bankers rounding. We had to write our own rounding method to get it to round 'properly'.

  • Hugo Kornelis (10/13/2010)


    It is your client software that converts this to a character representation so that you can see it on your screen. The client software chooses to represent these values with one trailing zero.

    (To check this, open an access project, create a pass-through query to execute this query on SQL Server, and heck the results. They'll show as 2.5 and 3.6. Or in my case as 2,5 and 3,6, since I have a Dutch version of Access, and the Dutch, like many other Europeans, use a decimal comma rather than a decimal point)

    I'd just like to point out that it isn't the version of Access that determines what decimal separator you get, it's the regional settings in the Control Panel.


    Just because you're right doesn't mean everybody else is wrong.

  • Interesting question. I thought all Microsoft software did banker's rounding. Thanks.

  • Daniel Bowlin (10/13/2010)


    Interesting question. I thought all Microsoft software did banker's rounding. Thanks.

    It is flexible. CLR procedures in SQL Server, any .NET code can use one of the two MidpointRounding enum values as a mode parameter value of the of the Math.Round method:

    Math.Round(4.5, 0, MidpointRounding.AwayFromZero); -- 5

    Math.Round(4.5, 0, MidpointRounding.ToEven); -- 4

    Oleg

  • Thanks for the comments.

    In a view SQL Server returned n.n where in query analyzer it returned n.n0 for each.

    My main point was to showcase that there is disparity in programs as to how rounding is performed. Logic or specifications taken at face value (Round the average tax to the nearest tenth) can create differences in results. If you have an application performing the rounding and storing it in the db and run a periodic db script to verify you may get answers that result in 'random' discrepancies.

    Tim

    There are only 10 types of people that understand binary, those that do and those that do not.

  • Thanks for the question.

    As a note I have always hated the round "to even" method that Microsoft sometimes uses.

  • Thanks for the QOTD

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic. Login to reply