Wednesday, March 26, 2008

SQL Server 2000 Rounding Errors

As I told in another article, the company I work for is installing an ERP software and I was given the task of migrating the invoice data from the old database to the new one.

Although both databases are SQL Server 2000 I got stuck with a rounding problem because the datatypes used by the databases are different.

On our old database we use the money datatype while the new database uses the float datatype.

I don't know how the ERP software deals with the values internally because they use yet another layer between the software and the database server, but the rounding problem remains.

I did some testing and came up with the table below.

As you can see there are some roundings that the float datatype does not handle very well.

So, I think that float should not be used when precision and robustness are required.

Edited to Add: On SQL Server 2005 the rounding problems with float apparently does not exist, as I did the same test that produced this table in SQL Server 2000 and I could not find any weirdness.

nmoneyfloat
0.00000.0
1.01000.01
2.02000.02
3.03002.9999999999999999E-2
4.04004.0000000000000001E-2
5.05005.0000000000000003E-2
6.06005.9999999999999998E-2
7.07007.0000000000000007E-2
8.08008.0000000000000002E-2
9.09008.9999999999999997E-2
10.10000.10000000000000001
11.11000.11
12.12000.12
13.13000.13
14.14000.14000000000000001
15.15000.14999999999999999
16.16000.16
17.17000.17000000000000001
18.18000.17999999999999999
19.19000.19
20.20000.20000000000000001
21.21000.20999999999999999
22.22000.22
23.23000.23000000000000001
24.24000.23999999999999999
25.25000.25
26.26000.26000000000000001
27.27000.27000000000000002
28.28000.28000000000000003
29.29000.28999999999999998
30.30000.29999999999999999
31.31000.31
32.32000.32000000000000001
33.33000.33000000000000002
34.34000.34000000000000002
35.35000.34999999999999998
36.36000.35999999999999999
37.37000.37
38.38000.38
39.39000.39000000000000001
40.40000.40000000000000002
41.41000.40999999999999998
42.42000.41999999999999998
43.43000.42999999999999999
44.44000.44
45.45000.45000000000000001
46.46000.46000000000000002
47.47000.46999999999999997
48.48000.47999999999999998
49.49000.48999999999999999
50.50000.5
51.51000.51000000000000001
52.52000.52000000000000002
53.53000.53000000000000003
54.54000.54000000000000004
55.55000.55000000000000004
56.56000.56000000000000005
57.57000.56999999999999995
58.58000.57999999999999996
59.59000.58999999999999997
60.60000.59999999999999998
61.61000.60999999999999999
62.62000.62
63.63000.63
64.64000.64000000000000001
65.65000.65000000000000002
66.66000.66000000000000003
67.67000.67000000000000004
68.68000.68000000000000005
69.69000.68999999999999995
70.70000.69999999999999996
71.71000.70999999999999996
72.72000.71999999999999997
73.73000.72999999999999998
74.74000.73999999999999999
75.75000.75
76.76000.76000000000000001
77.77000.77000000000000002
78.78000.78000000000000003
79.79000.79000000000000004
80.80000.80000000000000004
81.81000.81000000000000005
82.82000.81999999999999995
83.83000.82999999999999996
84.84000.83999999999999997
85.85000.84999999999999998
86.86000.85999999999999999
87.87000.87
88.88000.88
89.89000.89000000000000001
90.90000.90000000000000002
91.91000.91000000000000003
92.92000.92000000000000004
93.93000.93000000000000005
94.94000.93999999999999995
95.95000.94999999999999996
96.96000.95999999999999996
97.97000.96999999999999997
98.98000.97999999999999998
99.99000.98999999999999999

0 Comments:

Post a Comment