C# 内置 .Net 算法将值四舍五入到最接近的 10 区间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/274439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Built in .Net algorithm to round value to the nearest 10 interval
提问by Graviton
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.
如何在 C# 中将任何值舍入到 10 区间?例如,如果我有 11,我希望它返回 10,如果我有 136,那么我希望它返回 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.
但我正在寻找一种内置算法来完成这项工作,例如 Math.Round()。我不想手工完成的原因是我不想在我的项目中编写相同或相似的代码,即使是像上面这样简单的代码。
采纳答案by Chris Charabaruk
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.
类库中没有内置函数可以执行此操作。最接近的是System.Math.Round(),它仅用于将 Decimal 和 Double 类型的数字四舍五入到最接近的整数值。但是,如果您使用 .NET 3.5,您可以将您的语句封装在扩展方法中,这将使您能够更干净地使用该函数。
public static class ExtensionMethods
{
public static int RoundOff (this int i)
{
return ((int)Math.Round(i / 10.0)) * 10;
}
}
int roundedNumber = 236.RoundOff(); // returns 240
int roundedNumber2 = 11.RoundOff(); // returns 10
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:
如果您针对旧版本的 .NET 框架进行编程,只需从 RoundOff 函数中删除“this”,然后像这样调用该函数:
int roundedNumber = ExtensionMethods.RoundOff(236); // returns 240
int roundedNumber2 = ExtensionMethods.RoundOff(11); // returns 10
回答by Raymond Martineau
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.
将浮点数舍入为整数类似于 (int)(x+0.5),而不是简单地强制转换 x - 如果您想要 10 的倍数,您可以轻松地进行调整。
If you just want to do integer math and are rounding it to ten, try (x+10/2)/10*10.
如果您只想进行整数数学运算并将其四舍五入为 10,请尝试 (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.
编辑:我注意到这个回复不符合原作者的要求,也是一种我不想做的有偏见的四舍五入形式。然而,另一个被接受的回应已经说明了 Math.round(),一个更好的解决方案。
回答by Armstrongest
Use Math.Ceiling to always round up.
使用 Math.Ceiling 总是向上取整。
int number = 236;
number = (int)(Math.Ceiling(number / 10.0d) * 10);
Modulus(%) gets the remainder, so you get:
Modulus(%) 得到余数,所以你得到:
// number = 236 + 10 - 6
Put that into an extension method
将其放入扩展方法中
public static int roundupbyten(this int i){
// return i + (10 - i % 10); <-- logic error. Oops!
return (int)(Math.Ceiling(i / 10.0d)*10); // fixed
}
// call like so:
int number = 236.roundupbyten();
above edited: I should've gone with my first instinct to use Math.Ceiling
以上编辑:我应该用我的第一直觉去使用 Math.Ceiling
回答by Jronny
This might be a little too late but I guess this might be of good help someday...
这可能有点太晚了,但我想有一天这可能会有所帮助......
I have tried this:
我试过这个:
public int RoundOff(int number, int interval){
int remainder = number % interval;
number += (remainder < interval / 2) ? -remainder : (interval - remainder);
return number;
}
To use:
使用:
int number = 11;
int roundednumber = RoundOff(number, 10);
This way, you have the option whether if the half of the interval will be rounded up or rounded down. =)
这样,您可以选择是将间隔的一半向上舍入还是向下舍入。=)
回答by Dave
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.
我宁愿不带Math
入库也不去浮点,所以我的建议是像下面那样做整数运算,在那里我四舍五入到下一个 1K。如果您不想重复,请将其包装在方法或 lambda 代码段或其他内容中。
int MyRoundedUp1024Int = ((lSomeInteger + 1023) / 1024) * 1024;
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.
我还没有对此与其他方式进行性能测试,但我敢打赌这是最快的方法,除了这个版本的移位和旋转。
回答by Lucas925
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.
老问题,但这里有一种方法可以解决所问的问题,并且我将其扩展为能够将任何数字四舍五入为您想要的无花果数量。
private double Rounding(double d, int digits)
{
int neg = 1;
if (d < 0)
{
d = d * (-1);
neg = -1;
}
int n = 0;
if (d > 1)
{
while (d > 1)
{
d = d / 10;
n++;
}
d = Math.Round(d * Math.Pow(10, digits));
d = d * Math.Pow(10, n - digits);
}
else
{
while (d < 0.1)
{
d = d * 10;
n++;
}
d = Math.Round(d * Math.Pow(10, digits));
d = d / Math.Pow(10, n + digits);
}
return d*neg;
}
private void testing()
{
double a = Rounding(1230435.34553,3);
double b = Rounding(0.004567023523,4);
double c = Rounding(-89032.5325,2);
double d = Rounding(-0.123409,4);
double e = Rounding(0.503522,1);
Console.Write(a.ToString() + "\n" + b.ToString() + "\n" +
c.ToString() + "\n" + d.ToString() + "\n" + e.ToString() + "\n");
}