I have a variable as type float
in a SQL Server 2008 R2 table. I want to store 937687500 for that. I use a C# application.
but after I store data, I have 937687488.
What can I do?
I have a variable as type float
in a SQL Server 2008 R2 table. I want to store 937687500 for that. I use a C# application.
but after I store data, I have 937687488.
What can I do?
This is related to the nature of 32-bits float numbers. Floating point numbers will start to lose accuracy when you get into really high numbers. If you change to a 64 bit float instead you will get higher precision and that will work for this particualr number, but if you plan on inserting numbers that are really big, I would recomend using integer instead.
You can re-cast your float
Declare @F float = 937687500
Select AsFloat = cast(@F as varchar(25))
,AsInt = cast(cast(@F as int) as varchar(25))
,AsDec = cast(cast(@F as decimal) as varchar(25))
,AsNum = cast(cast(@F as numeric) as varchar(25))
Returns
AsFloat AsInt AsDec AsNum
9.37688e+008 937687500 937687500 937687500