views:

1413

answers:

3

I am working on a legacy ASP application. I am attempting to insert a value (40.33) into a field in SQL Server 2000 that happens to be a float type. Every place I can see (via some logging) in the application is sending 40.33 to the Stored Procedure. When I run SQL Profiler against the database while the call is happening, the value that I see in the trace is 4.033000183105469e+001

Where is all the extra garbage coming from (the 183105469)?

Why is it that when I pass in 40, or 40.25 there is nothing extra?

Is this just one of the weird side effects of using float? When I am writing something I normally use money or decimal or something else, so not that familiar with the float datatype.

A: 

I think this is probably just a precision issue - the 0.33 part of the number can't be represented exactly in binary - this is probably the closest that you can get to.

Terence Lewis
+6  A: 

Yes, this is a weird, although well-known, side effect of using FLOAT.

In Microsoft SQL Server, you should use exact numeric datatypes such as NUMERIC, DECIMAL, MONEY or SMALLMONEY if you need exact numerics with scale.

Do not use FLOAT.

Bill Karwin
+1; I was typing out (essentially) the same thing. :)
John Rudy
not so weird really... unless your math background is less than rigorous. ;-) Great answer
Something can be weird without being unknown or unexpected. :-)
Bill Karwin
A: 

The problem is that floats are not 100% accurate. If you need your numbers to be exact (especially when dealing with monetary values)... you should use a Decimal type.

bobwienholt
Decimal isn't 100% accurate either...