Difference in rounding in sql and clr
-
19-09-2019 - |
Question
I have searched and can not find the answer. I double checked the data types between SQL and CLR and I appear to have that correct. But I am getting a different result between using CLR and SQL. Not much, but enough to be off a penny. And that is not acceptable.
Example in VB.NET
Dim dLoanAmount As Decimal = 169500
Dim dNetDiscount As Decimal = 100.871
Dim dDiscountPremium As Decimal = (dLoanAmount * (dNetDiscount - 100.0) / 100.0)
Console.WriteLine("original amount is " + dDiscountPremium.ToString())
will display 1476.34499999999
Example in SQL
DECLARE @loanAmt decimal (20,10)
DECLARE @discount decimal (20,10)
SET @loanAmt = 169500.000000
SET @discount = 100.871000
select @loanAmt*(@discount-100.0)/100.0
that returns 1476.345000000000000
We have to use the VB for some documents, but for some file transfers we use sql. Anyone have any suggestions why this is?
cheers
bob
Solution
You're using double literals instead of decimal ones. Try this:
Dim dLoanAmount As Decimal = 169500D
Dim dNetDiscount As Decimal = 100.871D
Dim dDiscountPremium As Decimal = (dLoanAmount * (dNetDiscount - 100D) / 100D)
Console.WriteLine("original amount is " + dDiscountPremium.ToString())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow