Simple Calculus is not that simple? D:

  • Hi guys, sorry to bother with such a simple question, but i dont know whats wrong on this simple code.

    i keep getting Zero in the @D1/100 sections of the formula.

    here's the little code

    declare@D1 int, @D2 int, @D3 int, @D4 int, @D5 int, @TOTAL DECIMAL(10,2), @PRICE DECIMAL(10,2)

    select @D1=30, @D2=5, @D3=2,@D4=0, @D5=0, @TOTAL=100.00,@PRICE=0

    SET @PRICE = (@TOTAL *((1-(@D1/100))*

    (1-(@D2/100))*

    (1-(@D3/100))*

    (1-(@D4/100))*

    (1-(@D5/100))))

    i just get @TOTAL multiplying 1's, because de divisions are cero, but its a simple 30/100 in the first one for example, why i get zero?

    Am i missing something underground D:

    if u do it directly on numbers it works as it should be

    print 100*((1-.3)*(1-.05)*(1-.02)*(1-0)*(1-0))

    txs in advance for any reply.

    i feel like im gonna get punched in the face with soemthing so damn simple XD.

  • it's integer division that is killing you

    @integer / 100, if it is less than one, is zero. SQL server shortcuts the math and says int / int is an int result.

    @integer / 100.00 is a decimal, which is what you want...just change your denominators to make sure they are decimal constants.

    i get 65.17 with this formula:

    declare @D1 int, @D2 int, @D3 int, @D4 int, @D5 int, @TOTAL DECIMAL(10,2), @PRICE DECIMAL(10,2)

    select @D1=30, @D2=5, @D3=2,@D4=0, @D5=0, @TOTAL=100.00,@PRICE=0

    SET @PRICE = (@TOTAL *((1-(@D1/100.0))*

    (1-(@D2/100.0))*

    (1-(@D3/100.0))*

    (1-(@D4/100.0))*

    (1-(@D5/100.0))))

    print @PRICE

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Oh you're right!

    didn't know that implicit work of sql.

    another new thing learned today.

    Thanks a lot :w00t::w00t:

Viewing 4 posts - 1 through 3 (of 3 total)

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