Question : weird effects using ROUND() on REALs in TSQL

I am trying to add up a few columns which are reals, and round the result to 2x decimal places.

My columns are REALS, and I've been able to reproduce the bug using vars of type REAL.

Consider this code below which we expect to return 710.40

This returns:
710.39999999999998

yet if we remove the call to ROUND()
it returns:
710.40002

What is happening here ?
And How can I get an accurate rounding (to 2x decimal places) of these REALS ?

I am using SQL Server 2000.

Many thanks!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
DECLARE @r1 REAL, @r2 REAL, @r3 REAL, @r4 REAL, @r5 REAL, @r6 REAL, @r7 REAL, @r8 REAL, @r9 REAL, @r10 REAL, @r11 REAL
SET @r1 = 0.0
SET @r2 = 0.0
SET @r3 = 0.0
SET @r4 = 0.0
SET @r5 = 0.0
SET @r6 = 0.0
SET @r7 = 710.40002
SET @r8 = 0.0
SET @r9 = 0.0
SET @r10 = 0.0
SET @r11 = 0.0
SELECT ROUND((@r1 + @r2 + @r3 + @r4 + @r5 + @r6 + @r7 + @r8 + @r9 + @r10 + @r11), 2)

Answer : weird effects using ROUND() on REALs in TSQL

But as ralmada has pointed out you are using an approximate numeric that is not intended for that purpose, so you have a couple of options:
1. Change your code to use appropriate data types such as numeric(10, 5) to get consistent output.
2. Leave the code as is, and live with it or upgrade to 2005/2008 and hope you don't get bit again.

Pick your poison.
Random Solutions  
 
programming4us programming4us