• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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?




0
riouxnick
Asked:
riouxnick
  • 2
  • 2
1 Solution
 
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:
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
 
Éric MoreauSenior .Net ConsultantCommented:
And I found an old SQL Server 2000 installation and the same queries provided the same results.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now