.NET method to round a number up to the nearest multiple of another number?

后端 未结 4 945
南笙
南笙 2020-12-31 08:11

I\'m looking for a method that can round a number up to the nearest multiple of another. This is similar Quantization.

Eg. If I want to round 81 up to the

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 08:41

    Lee's answer is good but it should have been:

    t = m + n - 1; return (t - (t % m));
    

    Notice the change from N to M. The modulo operation should be done with the multiplier (m) and not with the number (n).

提交回复
热议问题