I'm trying to calculate percent off values for dollar amounts. At 50%, sometimes you get half a penny, and I need to round it to the nearest cent.
In Sql, my calculation is as follows:
round(retail * 0.5, 2, 0)
If I take the following values I get different results:
Unrounded I get:
When rounded I get:
I'm told this is a form of "banker's rounding", but it seems like the value that is affecting the rounding direction is the integer value.
My problem is that I expect the top value to round to 2.20. If this is indeed bankers rounding, and it is affected by the integer value here, does anyone have an example for preventing this behavior. If it is not bankers rounding, can someone please provide an explanation and potentially a solution to just get normal rounding behavior?
Thanks in advance!
What is the data type of retail
? I can't test this on SQL 2000, but on SQL 2008 R2, I see similar results if I use float
.
float: 4.39 --> 2.195, rounds to 2.19
2.49 --> 1.245, rounds to 1.25
money: 4.39 --> 2.195, rounds to 2.20
2.49 --> 1.245, rounds to 1.25
decimal(5,2): 4.39 --> 2.195, rounds to 2.20
2.49 --> 1.245, rounds to 1.25
To show the approximation created by float
, you can cast to decimal(20,19)
and it becomes clear why it's rounding this way:
4.39 * 0.5 --> 2.1949999999999999000
2.49 * 0.5 --> 1.2450000000000001000
Take a look at some rounding methods here: SQL Server Rounding Methods bankers rounding is explained
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With