Link to home
Start Free TrialLog in
Avatar of riouxnick
riouxnick

asked on

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?




Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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

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.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And I found an old SQL Server 2000 installation and the same queries provided the same results.