Conversion Rounding

  • I'm finding that in 2016 the script included at the bottom will return 3.27 for the value 3.275 but I would expect it to be 3.28 because the 5 in the thousandths decimal place would round up.  The other two values act as expected where a 4 rounds down and a 6 rounds up. Any version older than 2016 returns 3.28. Does anyone know why this would be?

    CREATE TABLE dbo.NumbersTbl (Cost FLOAT)

    INSERT INTO dbo.NumbersTbl
    VALUES (5.274)
    ,(3.275)
    ,(9.276)

    SELECT Cost
    ,convert(NUMERIC(5, 2), Cost)
    FROM dbo.NumbersTbl

     

    • This topic was modified 3 years, 10 months ago by  RonMexico.
    • This topic was modified 3 years, 10 months ago by  RonMexico.
  • I am guessing here - but it looks to be an issue with using float.  Since float is an approximate data type - the value of 3.275 is probably set to 3.27499999999...which is less than 5 and therefore rounded down.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • That part makes sense. Why does 2016 work differently than previous versions?

  • Different hardware - different OS - generating a different float value which is probably 3.275111111111111 or some other variant.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Understood. I wanted to make sure this wasn't part of some bigger issue or something of that nature. Thanks for the help.

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

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