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?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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.
Éric MoreauSenior .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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
And I found an old SQL Server 2000 installation and the same queries provided the same results.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.