7

there is some bizarre thing happening with my report generated in SQL Server Reporting Services and I hope I am not being too stupid. I use the Round function to get integers. Sometimes a 4.5 will round to 4 and a 5.5 will round to 6. Is this because of the rounding method? I am using this:

Round(Fields!GroupAverageAssessment.Value,0)

How can I make a regular rounding (4.5 to 5, 3.5 to 4, 6.5 to 7 and so on...)

Thanks

Marcos Buarque
  • 3,318
  • 8
  • 44
  • 46
  • 1
    What datatype is your original field? A float datatype might look like it is 4.5 but really be 4.49999999999 or something and the 5.5 might really be 5.500000000000001 – HLGEM Feb 04 '10 at 19:37
  • If *sometimes* in "sometimes a 4.5 will round to 4 and a 5.5 will round to 6" is actually *every time*, then it is using Banker's rounding. – Austin Salonen Feb 04 '10 at 19:42
  • Thanks, the field is defined as Double, so that should not be the problem. I will try awayfromzero rounding. – Marcos Buarque Feb 04 '10 at 21:47
  • @Marcos: double has exactly the same problem as float. It can be slightly inaccurate. This is probably not your problem here judging from your description, but it could be, and you certainly should investigate it if other solutions fail. – Mark Byers Feb 04 '10 at 22:03
  • Awayfromzero worked for me. Thank you for the help. – Marcos Buarque Feb 05 '10 at 03:05
  • @Marcos: See this for more background http://stackoverflow.com/questions/311696/why-does-net-use-bankers-rounding-as-default – Damon Feb 05 '10 at 03:29

2 Answers2

15

It sounds like round to even, also known as Banker's rounding.

The other option is "away from zero", which is what you want:

Round(4.5, 0, MidpointRounding.AwayFromZero)
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

Use the MidpointRounding.AwayFromZero option:

Round([calculated number], [num decimal places], MidpointRounding.AwayFromZero)
slugster
  • 49,403
  • 14
  • 95
  • 145