Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 float type data problem

Author  Topic 

vuthaianh
Starting Member

1 Post

Posted - 2008-07-06 : 22:36:56
Hi all,

I've got a problem when manipulating on float type data

Here is the code:

DECLARE @First float, @Second float, @Third float

SET @First = 1.22
SET @Second = 2.72
SET @Third = 3.94

SELECT @First + @Second - @Third

The result is "4.44089209850063E-16" instead "0"

How was that ?

Thanks a lot

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-06 : 22:58:33
float is approximate value. Use numeric or decimal instead if you don't required the high precision.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-07-07 : 06:09:00
For example...


DECLARE
@First DECIMAL(10,4)
, @Second DECIMAL(10,4)
, @Third DECIMAL(10,4)

SET @First = 1.22
SET @Second = 2.72
SET @Third = 3.94

SELECT @First + @Second - @Third


Produced 0.0000

The Numeric and Decimal types are specified by <type>(LENGTH,PRECISION). so (10,4) is 10 wide with 4 decimal places. (so 999999.9999 is the biggest number it can hold.

-------------
Charlie
Go to Top of Page
   

- Advertisement -