1

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.

enter image description here

but after I store data, I have 937687488.

What can I do?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Fatemeh
  • 177
  • 15

2 Answers2

0

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.

abydal
  • 378
  • 1
  • 8
  • Thank you very much. but how can I change to a 64 bit float. I can't find it in sql. in amount field properties size is 8 and disable – Fatemeh Oct 22 '16 at 09:15
  • I would suggest looking at http://stackoverflow.com/a/626904/6623706. The datatype you want to change to is _real_ – abydal Oct 22 '16 at 09:19
  • Thanks a lot . I changed it to real but value in data base stores 1.766554E+10 . It is scientific number. can you help me? – Fatemeh Oct 22 '16 at 09:43
  • I suspect that this a currency value. If this is the case, you should use `money` data type. Be aware that `real` and `float` are approximate numbers Read more here [Data Types (Transact-SQL)](https://technet.microsoft.com/en-us/library/ms187752.aspx), [float and real (Transact-SQL)](https://technet.microsoft.com/en-us/library/ms173773.aspx) and [decimal and numeric (Transact-SQL)](https://technet.microsoft.com/en-us/library/ms187746.aspx) – shadow Oct 22 '16 at 10:54
  • I use bigint. Thank you very much – Fatemeh Oct 22 '16 at 11:36
0

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
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66