内置 .Net 算法将值舍入到最接近的 10 区间
在 C# 中,如何将任意值舍入为 10 区间? 例如,如果我有 11,我希望它返回 10,如果我有 136,那么我希望它返回 140。
我可以轻松地手动完成
return ((int)(number / 10)) * 10;
但我正在寻找一个内置算法来完成这项工作,例如数学.Round(). 我不想手工完成的原因是我不想在我的项目中编写相同或相似的代码,即使是像上面这样简单的事情。
How to, in C# round any value to 10 interval? For example, if I have 11, I want it to return 10, if I have 136, then I want it to return 140.
I can easily do it by hand
return ((int)(number / 10)) * 10;
But I am looking for an builtin algorithm to do this job, something like Math.Round(). The reason why I won't want to do by hand is that I don't want to write same or similar piece of code all over my projects, even for something as simple as the above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
类库中没有内置函数可以执行此操作。 最接近的是 System.Math.Round()仅适用于将 Decimal 和 Double 类型的数字舍入到最接近的整数值。 但是,如果您使用的是 .NET 3.5,则可以将语句包装在扩展方法中,这将使您能够更干净地使用该函数。
如果您正在针对旧版本的 .NET 框架进行编程,只需从 RoundOff 函数中删除“this”,然后像这样调用该函数:
There is no built-in function in the class library that will do this. The closest is System.Math.Round() which is only for rounding numbers of types Decimal and Double to the nearest integer value. However, you can wrap your statement up in a extension method, if you are working with .NET 3.5, which will allow you to use the function much more cleanly.
If you are programming against an older version of the .NET framework, just remove the "this" from the RoundOff function, and call the function like so:
使用 Math.Ceiling 始终向上舍入。
Modulus(%) 得到余数,所以你得到:
将其放入上面编辑的扩展方法中
: 我应该本能地使用 Math.Ceiling
我 在计算 UPC 校验位时在博客上讨论了这一点。
Use Math.Ceiling to always round up.
Modulus(%) gets the remainder, so you get:
Put that into an extension method
above edited: I should've gone with my first instinct to use Math.Ceiling
I blogged about this when calculating UPC check digits.
这可能有点太晚了,但我想有一天这可能会有很好的帮助...
我已经尝试过:
使用:
这样,您可以选择是否将间隔的一半向上舍入或向下舍入。 =)
This might be a little too late but I guess this might be of good help someday...
I have tried this:
To use:
This way, you have the option whether if the half of the interval will be rounded up or rounded down. =)
将浮点数舍入为整数类似于 (int)(x+0.5),而不是简单地转换 x - 如果您想要 10 的倍数,您可以轻松调整。
如果您只想进行整数数学运算并将其四舍五入为 10,请尝试 (x+10/2)/10*10。
编辑:我注意到这个回复不符合原作者的要求,也是一种我不喜欢做的有偏见的四舍五入形式。 然而,另一个接受的回复已经指出了 Math.round(),这是一个更好的解决方案。
Rounding a float to an integer is similar to (int)(x+0.5), as opposed to simply casting x - if you want a multiple of 10, you can easily adapt that.
If you just want to do integer math and are rounding it to ten, try (x+10/2)/10*10.
Edit: I noticed that this response doesn't meet the original's author's request, and is also a biased form of rounding that I prefer not to do. However, another accepted response already stated Math.round(), a much better solution.
老问题,但这里有一种方法可以完成所要求的任务,而且我将其扩展为能够将任何数字四舍五入到您想要的符号数。
Old question but here is a way to do what has been asked plus I extended it to be able to round any number to the number of sig figs you want.
我不喜欢引入 Math 库,也不使用浮点,所以我的建议是只进行整数算术,如下所示,我四舍五入到下一个 1K。 如果您不想重复,请将其包装在方法或 lambda 片段或其他内容中。
我还没有对这种方式与其他方式进行性能测试,但我敢打赌这是最快的方式来做到这一点,保存可能是这种方式的移位和旋转位版本。
I prefer to not bring in the
Math
library nor go to floating point so my suggestion is just do integer arithmetic like below where I round up to the next 1K. Wrap it in a method or lambda snippet or something if you don't want to repeat.I have not run performance tests on this vs. other the ways but I'd bet it is the fastest way to do this save maybe a shifting and rotating of bits version of this.
以下是我如何舍入到任意因子的最接近倍数,而不从整数类型转换为浮点值。 这适用于从
int.MinValue + 1
到int.MaxValue
的任何 int我使用了 从零方程舍入一半,
Round(x) = sgn(x)*Floor(Abs(x) + 0.5)
,事实Floor(z) = z - (z%1)
和我想要的输出方程F(value, factor) = Round(value/factor)*factor
来推导不需要精确小数除法的方程。下面是整个扩展方法类,其中包含
int
和long
的方法以及仅朝零或远离零舍入的方法。Here's how I round to the nearest multiple of any arbitrary factor without converting from integral types to floating-point values. This works for any int from
int.MinValue + 1
toint.MaxValue
I used the Round half away from zero equation,
Round(x) = sgn(x)*Floor(Abs(x) + 0.5)
, the fact thatFloor(z) = z - (z%1)
, and my desired output equationF(value, factor) = Round(value/factor)*factor
to derive the an equation that doesn't require precise decimal division.Here's the entire extension method class with methods for both
int
andlong
as well as methods to round only toward or away from zero.据我所知,没有一个原生的内置 C# 库可以将整数四舍五入到最接近的十位。
如果您使用的是 c# 8 或更高版本,您可以创建小型开关表达式实用程序方法,这些方法可以做很多很酷的有用的事情。 如果您使用的是旧版本,则可以使用
if/else
和switch case
块:您必须在使用它的任何地方导入它,但这会任何库都是如此,除非有办法将类添加到全局名称空间。
As far as I know there isn't a native built-in c# library that rounds integers to the nearest tens place.
If you're using c# 8 or later you can create small switch expression utility methods that can do a lot of cool helpful things. If you're using an older version,
if/else
andswitch case
blocks can be used instead:You would have to import it where ever you use it but that'd be the case with any library unless there's a way to add classes to a global name space.