Rounding Error in SQL Server 2000

Posted on 2008-11-11
Last Modified: 2012-06-27
I have an odd issue. I am rounding an expression in a view in SQL Server 2000. It keeps rounding the 5's down instead of rounding them up. The issue is really ghosty and behaves strangely. Here is the example of why it is so strange:

When running the following: (The numbers .0825 and 1090 are really table column references, but are replaced here with values for easier discussion)

SELECT     ROUND((CAST(.0825 AS FLOAT(10, 4)) + 1) * CAST(1090 AS Float(10, 4)), 2)

I get the answer: 1179.92.

Now, if I run the following:

SELECT (1.0825*1090,2)

I get the answer 1179.93.

The second answer would obviously be the correct arith. method of rounding 1179.925. I want the outcome of 1179.93, so I need to alter the first query. The problem with this is that the values that are being pulled by the query require all of those Cast functions to properly format the input for the calculation.

Why is this a problem? I understand bank rounding and all of that garbage, but why is the Round() function doing different things with the same inputs?

Question by:riouxnick
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    yiou cannot specify anything for "FLOAT" data type, so the (10,4) is syntax incorrect, AFAIK

    anyhow, try DECIMAL:
    SELECT     ROUND((CAST(.0825 AS FLOAT) + 1) * CAST(1090 AS Float), 2) 
    SELECT     ROUND((CAST(.0825 AS DECIMAL(10,4)) + 1) * CAST(1090 AS DECIMAL(10,4)), 2)

    Open in new window

    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    PS: stay away from FLOAT whenever you can. it's an unprecise data type, the only case you use it is when you need at the same time very large OR very small values, for example for scientific/maths figures.

    for money amounts and the like, use decimal.
    LVL 69

    Accepted Solution

    I just ran these 2 queries on SQL 2005 and they both gave the correct answer:
    SELECT  ROUND((CAST(.0825 AS numeric(10, 4)) + 1) * CAST(1090 AS numeric(10, 4)), 2)
    SELECT round(1.0825*1090,2)

    I needed to add ROUND on the second query and changed FLOAT to NUMERIC on the first one.
    LVL 69

    Expert Comment

    by:Éric Moreau
    And I found an old SQL Server 2000 installation and the same queries provided the same results.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now