The Math.Round() in VS.NET and Round() in SQL Server 2000
-
I have a decimal value 23.25 . When i use the math.round(23.25,1) in VS.NET, the result is 23.2 but with the function: round(23.25,1) in SQL Server 2000, the result is 23.3 :confused: How can i let them return the same value ? Thanks!
SQL and .Net use different rounding algorithms (as you have observed), ao the answer is that you can't. Net's version is the more 'proper' algorithm: it returns the nearest even number when the value is half way between (.25 will round to .2, .35 will round to .4) This yeilds more accurate averages and totals for the rounded data, and so is prefered. It is sometimes called "Banker's rounding". SQL always rounds to the next larger number for the mid value, which leads to totals and averages that are biased to the high side. I would recommend choosing the rounding behavior that you prefer, and always doing the rounding that way (i.e. either have ONLY SQL do the rounding, or ONLY .Net). I suspect that returning unrounded values from SQL and doing the rounding in .Net would be marginally more performant. Absolute faith corrupts as absolutely as absolute power Eric Hoffer The opposite of the religious fanatic is not the fanatical atheist but the gentle cynic who cares not whether there is a god or not. Eric Hoffer