Rounding not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Faye

    Rounding not working

    When I

    SELECT CAST(96.58 AS DECIMAL(10 , 0)), it returns 97.

    When I

    SELECT CAST(575629 / 54 AS DECIMAL(10 , 0)), why it returns 10659? It
    should return 10660, right?

    What am I missing?

    Thanks,

    Faye Larson
  • Plamen Ratchev

    #2
    Re: Rounding not working

    Running this will explain it:

    SELECT 575629 / 54

    When integer dividend is divided by an integer divisor the result is an
    integer that has any fractional part of the result truncated, so you get
    10659.

    But try this:
    SELECT CAST(575629 / 54.0 AS DECIMAL(10 , 0))

    or

    SELECT CAST(1.0 * 575629 / 54 AS DECIMAL(10 , 0))

    Those will give you the expected result 10660, because the higher precedence
    argument data type is the resulting data type of the division.

    HTH,

    Plamen Ratchev


    Comment

    Working...