Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2007
    Posts
    4

    Unanswered: Strange ROUND Function!!!

    Code:
    Set @AVG = ROUND ((V1*5+V2*5)/10,0)
    What is the difference between these codes?

    Code:
    Set @AVG = ROUND ((V1*5+V2*5)/10.0,0)
    For values V1=80, V2=85;
    First code gives 82! Second gives 83!

  2. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    The first one does an implicit rounding (actually truncating), because all of the values involved are defined as integer values.

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Not strange at all, once you understand the behavior of datatypes.

    In this example, only the integer portion is returned, because all parameters are integers. So rounding the result is superfluous:
    Code:
    select	825/10
    Even if the result is close to the next highest integer, it is always truncated:
    Code:
    select	829/10
    In this example, a decimal value is returned, since at least one parameter is a decimal datatype:
    Code:
    select	825/10.00
    ...and any decimal value greater than or equal to 0.5 is rounded up:
    Code:
    select	round(825/10.00, 0)
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  4. #4
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    Have a look at "Precision, Scale, and Length" in BOL. It gives a pretty good explanation... one that even I could understand.
    Inspiration Through Fermentation

  5. #5
    Join Date
    Feb 2007
    Posts
    4
    Thanx.
    using A/10 instead of A/10.0 wiped out someones confidence on me...

  6. #6
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Tell them you were only off by zero.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  7. #7
    Join Date
    Feb 2007
    Posts
    4
    Quote Originally Posted by blindman
    Tell them you were only off by zero.
    Thanx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •