8

I have a simple web app , and want to save some numbers of Float or Double format in SQL server.

but there is a problem , when I try to save 123.66 , In Table I see 123.6600003662109 stored.

How nd why my float number changed when save on DB? how can I fix this error?

Thanks

gbn
  • 422,506
  • 82
  • 585
  • 676
Ashian
  • 521
  • 2
  • 7
  • 22

3 Answers3

16

You're not actually trying to save 123.66, because you can't represent 123.66 exactly as a float or double. The database is saving the data more accurately than you're used to, that's all.

If you want to save decimal numbers accurately, use the decimal type.

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

This is not a database issue, but a general problem with floating numbers

gbn
  • 422,506
  • 82
  • 585
  • 676
0

I think this is VS Problem not Sql server, Here i pass amount in string ("12.89") to stored procedure and find data save accurate 12.89

Solution-

Change Float to string in Properties and Methods,procedure

Note

don't change column type Float to string 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 is Float type in table don't change to sting


Create Proc sp_Add
(

@amount varchar(20)

)
as

begin

Insert into Price(amount) values (@amount)

end
Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
  • Please edit your answer to fix the code formatting. Have a read of the guide on how to do this. – jwpfox Sep 06 '16 at 11:29