Wednesday, June 20, 2012

Float rounding issue involving sum

Try this in MS SQL:
create table #TEST (val1 float, val2 float)
insert into #TEST(val1, val2) values(0.0002, 0.0048)
select ROUND(val1 + val2, 2), val1+val2, ROUND(0.005,2)  from #TEST
drop table #TEST

val1 = 0.0002
val2 = 0.0048

The above query will return:
ROUND(val1 + val2, 2) = 0
val1 + val2 = 0.005
ROUND(0.005,2) = 0.010


It seems not just sum and rounding, even the multiplication itself will have issue


Dunno how to solve this yet, if u know, pls let me know.
Thanks.

No comments: