Store float values ​​in SQL Server

I have a simple web application and want to store some Float or Double numbers in SQL server.

but there is a problem when I try to save 123.66, in the table I see 123.6600003662109.

How and why did my floating point number change when saving to the database? How can I fix this error?

thanks

+6
floating-point c # sql sql-server-2005
source share
3 answers

You are not actually trying to save 123.66 because you cannot represent 123.66 exactly as a float or double. A database stores data more accurately than you are used to, that's all.

If you want to preserve decimal numbers exactly, use the decimal type.

See my articles on floating binary point and floating decimal point for more information.

+11
source share

This is not a database problem , but a common problem with floating numbers

+4
source share

I think this is VS. The problem is not the Sql server. Here I pass the amount in the line ("12.89") to the stored procedure and find the data, keeping the exact 12.89

Decision -

Change float to string in properties and methods procedure

Note

do not change float column type for row in table

Example -

In your property

 Public Float Amount { get; set; } 

TO

 Public String Amount { get; set; } 

In your method

 Public boolAdd(String amount) { //Your Logic Like bool status = false; DbParam[] param = new DbParam[1]; param[0] = new DbParam("@amount", "", "amount", SqlDbType.VarChar); status = Db.Update(ds, "sp_Add", "", "", param, true); return status; } 

In your procedure

Note-Amount column - Float type in the table does not change to sting


 Create Proc sp_Add ( @amount varchar(20) ) as begin Insert into Price(amount) values (@amount) end 
0
source share

All Articles