Rounding problem in sql 2008

  • andre-394971 (1/29/2012)


    Hello There,

    The solution proposed works as expected BUT I carnt use it as is.

    I need to be able to perform a statement similar to the below

    Select Name,Address, Function(Amount) as Amount.....

    Many Thanks

    The modulo function works just fine. You could have the following...

    SELECT Name, Address, Amount = Amount - Amount % .05

    ... and avoid the overhead of a function altogether.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Sorry I did not "see" you solution at all...my bad

    Do you have the time to explain the logic of how/why it works

    Many thanks

  • I'ts not my solution. JLS posted it.

    It uses the MODULO function (% operator) which simply returns the remainder of a division. Try running just Amount % .05 and see what it gives you. You will understand once you see that.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (1/30/2012)


    I'ts not my solution. JLS posted it.

    It uses the MODULO function (% operator) which simply returns the remainder of a division. Try running just Amount % .05 and see what it gives you. You will understand once you see that.

    As a small warning, you may want to confirm what your requirements would be when the number is negative (assuming that would even be allowable). Depending on which way your requirements go - you may need to use FLOOR vs cast or modulo.

    notice the output differences when the input is negative:

    select cast(-0.999 / 0.05 as int)*.05, floor(-0.999 / 0.05)*.05

    , -0.999-(-0.999)%.05

    select cast(0.999 / 0.05 as int)*.05, floor(0.999 / 0.05)*.05

    , 0.999-(0.999)%.05

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • I see πŸ™‚

    Thanks again

  • Thats interesting. You are correct ...there is a small chance that the numbers can be negitive.

    Thankyou again

  • I like JLS solution because it’s sleek. However here is another version for simplicity (takes care of negative numbers as well)...

    /*

    158.94 should give me 158.90

    158.95 should give me 158.95

    158.96 should give me 158.95

    158.97 should give me 158.95 etc

    */

    with cte_num (num)

    as

    (

    select 158.94

    union all

    select 158.95

    union all

    select 158.96

    union all

    select 158.97

    union all

    select -158.97

    union all

    select -158.94

    )

    select num,

    case when num < 0 and (num * (-100) % 10) >= 5 then round(num-1,1) + 0.05 -- Negative Numbers

    when num < 0 then round(num,1) -- Negative Numbers

    when (num * 100 % 10) >= 5 then round(num,1) - 0.05 -- final cent figure is >= 5

    else round(num,1) -- final cent figure is <=4

    end roundup

    from cte_num

  • SELECT NUM, SIGN(NUM)* FLOOR(ABS(NUM) * 20)/20 AS ROUNDUP

    This will handle negative and positive values in the same manner

  • I like that...even simpler again....

    Many Thanks....ill do some testing and use this solution...easier to read the sql script...which for my query is quite complicated

  • Read the prior post by Jeff Moden. Using it

    DECLARE @Amount Money

    SET @Amount = -158.98

    SELECT @Amount = @Amount - @Amount % .05

    SELECT @Amount

    Result:

    (No column name)

    -158.95

    DECLARE @Amount Money

    SET @Amount = -$201.83

    SELECT @Amount = @Amount - @Amount % .05

    SELECT @Amount

    Result:

    (No column name)

    -201.80

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 10 posts - 16 through 24 (of 24 total)

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