Rounding Error in SQL Server 2000

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?




riouxnickAsked:
Who is Participating?
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
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.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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.
0
 
Éric MoreauSenior .Net ConsultantCommented:
And I found an old SQL Server 2000 installation and the same queries provided the same results.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.