I have an application that computes a value of -1407.858; the C# data type for this is decimal.
The column data type in the database is decimal(12,2). I would’ve thought it’d round the value and insert -1407.86 (which would satisfy both away from zero rounding and to even rounding).
Unfortunately the value is being inserted as -1407.85. The only explanation I can come up for this is that the last digit is being truncated.
I have done some simple test queries in SQL Server such as:
declare @first AS decimal(12,2)
declare @second AS float --[or decimal(12,3)]
set @second = -1407.858
set @first=@second
select @first;
And I get back -1407.86, so I suppose SQL Server automatically rounds values when it’s setting them, but not when inserting them. Is this correct? Does SQL Server only truncate when inserting, but round when setting?
I did a test with Linq2sql, with the following situation:
Executing SQL Profiler, I realized linq2sql has truncated the value, sending the following statement to server:
So you are facing a linq2sql bug. You should round your numbers before you send them.