简体   繁体   English

C# - 舍入整数除法

[英]C# - Rounding the division of integers

Windows, C#, VS2010. Windows,C#,VS2010。

My app has this code: 我的应用有这个代码:

int[,] myArray=new int[10,2];
int result=0;
int x=0;
x++;

Like below, if the result is between 10.0001 and 10.9999; 如下所示,如果结果在10.0001和10.9999之间; result=10 结果= 10

result= (myArray[x,0]+myArray[x+1,0])/(x+1); 

I need this: if the result>=10&&result<10.5 rounds to 10. if the result between >=10.500&&<=10.999 rounds to 11. 我需要这个:如果结果> = 10 &&结果<10.5轮到10.如果结果> = 10.500 && <= 10.999轮到11。

Try the codes below. 请尝试以下代码。 But not worked. 但没有奏效。

result= Math.Round((myArray[x,0]+myArray[x-1,0])/(x+1));

Error: The call is ambiguous between the following methods or properties: 'System.Math.Round(double)' and 'System.Math.Round(decimal)' 错误:以下方法或属性之间的调用不明确:'System.Math.Round(double)'和'System.Math.Round(decimal)'

Error: Cannot implicitly convert type 'double' to 'int'. 错误:无法将类型'double'隐式转换为'int'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否错过了演员?)

result= Convert.ToInt32(Math.Round((myArray[x,0]+myArray[x-1,0])/(x+1)));

Error: The call is ambiguous between the following methods or properties: 'System.Math.Round(double)' and 'System.Math.Round(decimal)' 错误:以下方法或属性之间的调用不明确:'System.Math.Round(double)'和'System.Math.Round(decimal)'

Thanks in advance, ocaccy pontes. 在此先感谢,ocaccy pontes。

Try 尝试

result= (int)Math.Round((double)(myArray[x,0]+myArray[x-1,0])/(x+1));

That should iron out your compiler errors. 这应该解决你的编译器错误。

The first one ("Error: The call is ambiguous between the following methods or properties: 'System.Math.Round(double)' and 'System.Math.Round(decimal)'") is resolved by converting the dividend to a double , which 'trickles down' such that the output of the division is also a double to avoid loss of precision. 第一个(“错误:以下方法或属性之间的调用不明确:'System.Math.Round(double)'和'System.Math.Round(decimal)'”)通过将被除数转换为double来解决“涓滴”使得除法的输出也是 double以避免精度损失。

You could also explicitly convert the function argument to a double for the same effect: 您还可以将函数参数显式转换为double以获得相同的效果:

Math.Round((double)((myArray[x,0]+myArray[x-1,0])/(x+1)));

(Note the placement of the parentheses). (注意括号的位置)。

The second error ("Error: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)") is fixed by explicitly converting the return value of Math.Round to an int . 第二个错误(“错误:无法将类型'double'隐式转换为'int'。存在显式转换(您是否缺少转换?)”)通过显式将Math.Round的返回值转换为intMath.Round

I know this is a 3 year old question, but this answer seems to work well. 我知道这是一个3岁的问题,但这个答案似乎运作良好。 Maybe someone will find these extension methods of value. 也许有人会发现这些扩展方法的价值。

// Invalid for Dividend greater than 1073741823.
public static int FastDivideAndRoundBy(this int Dividend, int Divisor) {
    int PreQuotient = Dividend * 2 / Divisor;
    return (PreQuotient + (PreQuotient < 0 ? -1 : 1)) / 2;
}

// Probably slower since conversion from int to long and then back again.
public static int DivideAndRoundBy(this int Dividend, int Divisor) {
    long PreQuotient = (long)Dividend * 2 / Divisor;
    return (int)((PreQuotient + (PreQuotient < 0 ? -1 : 1)) / 2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM