Rounding up a number to nearest multiple of 5

后端 未结 21 2037
感动是毒
感动是毒 2020-11-27 15:24

Does anyone know how to round up a number to its nearest multiple of 5? I found an algorithm to round it to the nearest multiple of 10 but I can\'t find this one.

T

21条回答
  •  孤街浪徒
    2020-11-27 16:05

    int roundUp(int n, int multipleOf)
    {
      int a = (n / multipleOf) * multipleOf;
      int b = a + multipleOf;
      return (n - a > b - n)? b : a;
    }
    

    source: https://www.geeksforgeeks.org/round-the-given-number-to-nearest-multiple-of-10/

提交回复
热议问题