Why doesn't this 'select round(5/2,0)' output 3?

  • I cannot believe I am having this much trouble with rounding.

    Why doesn't

    select round(5/2,0)

    result in a 3?

    or this

    select round(5/2,1)

    result in a 2.5?

    I know this is pathetic. I've been trying to work on a formula in my code and I had to keep simplifying to make sure I understood exactly what's happening. And I got down to THAT simple and I am still confused.

  • the integer division, 5,2 is evaluated before the round occurs.

    if you divide two integers in SQL, the result is returned in the same datatype...an integer.

    That behavior is kind of unique to SQL; you do not see that in programming languages.

    to get 2.5-->3.0 , you have to cast/convert at least one of the two numbers to a decimal

    select round(5/2,0),round(5.0/2,0)

    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!

  • Thank you Lowell!

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

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