sql server - Incorrect value being passed in to SQL from .NET sqlcommand -
why when run below code, should insert value of 122387593.6 table, insert value of 122387593.59999999?
i have traced call made .net , can see value being passed in 122387593.59999999 sql, yet while debugging parameter value returned 122387593.6?!
i have not been able work out why .net doing - appreciated.
---sql code create table [dbo].[tblnum]( [num] [numeric](28, 8) not null ) on [primary] create procedure spnuminsert (@num numeric(28,8)) begin insert tblnum values(@num) end go --.net code dim = double.parse("122387593.6", globalization.cultureinfo.invariantculture) dim con sqlconnection = new sqlconnection("server=server;database=database;trusted_connection=true;") dim com sqlcommand = new sqlcommand("spnuminsert", con) com.commandtype = commandtype.storedprocedure com.parameters.add(new sqlparameter("@num", a)) con.open() com.executenonquery() con.close() con.dispose()
double
means ieee754. ieee754 cannot express every single value. quite possible 122387593.6
does not exist in ieee754, , 122387593.59999999
nearest approximation.
if need values match meat-headed humans tend agree on "exact" approximation (note contradiction in terms), should using decimal
, not double
.
to clear: both decimal
, double
forced approximate - there can fit such range of possible values finite bytes. way approximate different. decimal
appropriate discrete measurements money; double
appropriate continuous measurements.