2

After following the below mentioned links:

Link1 Link2 Link3 And the list goes on. I don't think I fully grasp the idea of Math.Round.

Lets say I have the following values [73.59, 46.28] now I want to road both up to the next 5.

After using Math.Round(Value / 5) * 5 the end results are as follow is:

73.59 => 70

46.28 => 45

This is working for how the Math.Round was intended to be used. However, I am working with currency and would like it to be the next 5 up. So the desired result would want to be the following:

73.59 => 75

46.28 => 50

I also tried playing around with MidpointRounding.AwayFromZero but I still do not get the desired result. Can somebody please explain or provide guidance to how I could accomplish this task?

Thank you in advance.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Ruben Redman
  • 489
  • 1
  • 6
  • 21
  • @xdtTransform that's not relevant - this question is about rounding to *5*, not the nearest integer, no matter what the rounding strategy. Multiplying by 5 is no accident - it probably came from a relevant duplicate [like this one](https://stackoverflow.com/questions/1531695/round-to-nearest-five) – Panagiotis Kanavos Oct 30 '19 at 09:57
  • 2
    So instead of `Math.Round(Value / 5) * 5` you want `Math.Ceiling(Value / 5) * 5` – Klaus Gütter Oct 30 '19 at 09:58
  • When mod of 5 is greater than 0, then add 5 and subtract mod of 5? – Markus Zeller Oct 30 '19 at 09:59
  • related : https://stackoverflow.com/questions/4846493/how-to-always-round-up-to-the-next-integer – xdtTransform Oct 30 '19 at 10:06

1 Answers1

5

To always round up, you should use Math.Ceiling instead of Math.Round:

static int RoundUpToMultipleOf5(decimal value) => (int)Math.Ceiling(value / 5) * 5
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36