如何在C#中四舍五入到最接近的整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8844674/
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
How to Round to the nearest whole number in C#
提问by SOF User
How can I round values to nearest integer?
如何将值四舍五入到最接近的整数?
For example:
例如:
1.1 => 1
1.5 => 2
1.9 => 2
"Math.Ceiling()" is not helping me. Any ideas?
“Math.Ceiling()”对我没有帮助。有任何想法吗?
采纳答案by Only Bolivian Here
See the official documentationfor more. For example:
更多内容请查看官方文档。例如:
Basically you give the Math.Roundmethod three parameters.
基本上你给Math.Round方法三个参数。
- The value you want to round.
- The number of decimals you want to keep after the value.
- An optional parameter you can invoke to use AwayFromZero rounding. (ignored unless rounding is ambiguous, e.g. 1.5)
- 要舍入的值。
- 要保留在值后的小数位数。
- 您可以调用以使用 AwayFromZero 舍入的可选参数。(忽略除非舍入不明确,例如 1.5)
Sample code:
示例代码:
var roundedA = Math.Round(1.1, 0); // Output: 1
var roundedB = Math.Round(1.5, 0, MidpointRounding.AwayFromZero); // Output: 2
var roundedC = Math.Round(1.9, 0); // Output: 2
var roundedD = Math.Round(2.5, 0); // Output: 2
var roundedE = Math.Round(2.5, 0, MidpointRounding.AwayFromZero); // Output: 3
var roundedF = Math.Round(3.49, 0, MidpointRounding.AwayFromZero); // Output: 3
You need MidpointRounding.AwayFromZeroif you want a .5 value to be rounded up. Unfortunately this isn't the default behavior for Math.Round(). If using MidpointRounding.ToEven(the default) the value is rounded to the nearest evennumber (1.5is rounded to 2, but 2.5is also rounded to 2).
你需要MidpointRounding.AwayFromZero,如果你想进行四舍五入一个0.5的值。不幸的是,这不是Math.Round(). 如果使用MidpointRounding.ToEven(默认值),则该值将四舍五入到最接近的偶数(1.5四舍五入为2,但2.5也四舍五入为2)。
回答by Thomas Levesque
回答by dasblinkenlight
You need Math.Round, not Math.Ceiling. Ceilingalways "rounds" up, while Roundrounds up or down depending on the value after the decimal point.
你需要Math.Round,不是Math.Ceiling。Ceiling总是“四舍五入”,而Round根据小数点后的值向上或向下四舍五入。
回答by user496607
You have the Math.Round function that does exactly what you want.
你有 Math.Round 函数,它完全符合你的要求。
Math.Round(1.1) results with 1
Math.Round(1.8) will result with 2.... and so one.
回答by devrooms
回答by Marlon
You can use Math.Round as others have suggested (recommended), or you could add 0.5 and cast to an int (which will drop the decimal part).
您可以按照其他人的建议(推荐)使用 Math.Round,或者您可以添加 0.5 并转换为 int(这将删除小数部分)。
double value = 1.1;
int roundedValue = (int)(value + 0.5); // equals 1
double value2 = 1.5;
int roundedValue2 = (int)(value2 + 0.5); // equals 2
回答by davogotland
there's this manual, and kinda cute way too:
有这本手册,还有点可爱的方式:
double d1 = 1.1;
double d2 = 1.5;
double d3 = 1.9;
int i1 = (int)(d1 + 0.5);
int i2 = (int)(d2 + 0.5);
int i3 = (int)(d3 + 0.5);
simply add 0.5 to any number, and cast it to int (or floor it) and it will be mathematically correctly rounded :D
只需将 0.5 加到任何数字上,然后将其转换为 int(或将其整平),它将在数学上正确四舍五入:D
回答by rick
this will round up to the nearest 5 or not change if it already is divisible by 5
这将四舍五入到最接近的 5 或者如果它已经可以被 5 整除则不会改变
public static double R(double x)
{
// markup to nearest 5
return (((int)(x / 5)) * 5) + ((x % 5) > 0 ? 5 : 0);
}
回答by user2970629
I was looking for this, but my example was to take a number, such as 4.2769 and drop it in a span as just 4.3. Not exactly the same, but if this helps:
我一直在寻找这个,但我的例子是取一个数字,比如 4.2769,然后把它放在一个跨度中,就像 4.3。不完全相同,但如果这有帮助:
Model.Statistics.AverageReview <= it's just a double from the model
Then:
然后:
@Model.Statistics.AverageReview.ToString("n1") <=gives me 4.3
@Model.Statistics.AverageReview.ToString("n2") <=gives me 4.28
etc...
等等...
回答by Kosmas
Just a reminder. Beware for double.
只是提醒。小心双重。
Math.Round(0.3 / 0.2 ) result in 1, because in double 0.3 / 0.2 = 1.49999999
Math.Round( 1.5 ) = 2

