.NET方法将一个数字四舍五入到另一个数字的最接近倍数? [英] .NET method to round a number up to the nearest multiple of another number?

查看:100
本文介绍了.NET方法将一个数字四舍五入到另一个数字的最接近倍数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,可以将数字向上舍入到另一个的最接近的倍数。这类似于量化。

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

例如。如果我想将81舍入到最接近的20的倍数,它应该返回100。

Eg. If I want to round 81 up to the nearest multiple of 20, it should return 100.

.NET框架中是否可以使用内置方法内置为此?

Is there a method built-in method in the .NET framework I can use for this?

我要求使用内置方法的原因是因为很有可能已经对其进行了优化。

The reason I'm asking for a built-in method is because there's a good chance it's been optimized already.

推荐答案

是的,整数运算。

要将m舍入为n的下一个倍数,请使用(( m + n-1)/ n)* n

To round m up to the next multiple of n, use ((m+n-1)/n)*n

这篇关于.NET方法将一个数字四舍五入到另一个数字的最接近倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆