为什么 Math.Round 并不总是四舍五入
Why Math.Round doesn't always rounding doubles
int fieldGoals = int.Parse(Console.ReadLine());
int fieldGoalAttempts = int.Parse(Console.ReadLine());
int threePointFieldGoals = int.Parse(Console.ReadLine());
int turnovers = int.Parse(Console.ReadLine());
int offensiveRebounds = int.Parse(Console.ReadLine());
int opponentDefensiveRebounds = int.Parse(Console.ReadLine());
int freeThrows = int.Parse(Console.ReadLine());
int freeThrowAttempts = int.Parse(Console.ReadLine());
double eFG = Math.Round( (fieldGoals + 0.5 * threePointFieldGoals) / fieldGoalAttempts );
double TOV = Math.Round( turnovers / (fieldGoalAttempts + 0.44 * freeThrowAttempts + turnovers) );
double ORB = Math.Round( offensiveRebounds / (offensiveRebounds + opponentDefensiveRebounds) );
double FT = Math.Round( freeThrows / fieldGoalAttempts );
问题出在 double ORB
和 double FT
。
出于某种原因,我无法对它们使用 Math.Round
。它说:
the call is ambiguous between the following methods or properties :
"Math.Round(double)" and "Math.Round(decimal)".
我只是不明白为什么前两个有效而后两个无效。
您正在使用 int
值调用 Math.Round
。您可能想先将它们转换为 double
:Math.Round( 1.0 * freeThrows...)
.
没有 Math.Round(int)
重载,但是 double
和 decimal
有重载,并且 int
可以隐式转换为两者。因此调用会产生歧义。
您尝试除以整数 - 结果将是整数。所以,你不能四舍五入这个数字。在除法和舍入之前将其转换为双精度:
double ORB = Math.Round( (double)offensiveRebounds / (offensiveRebounds + opponentDefensiveRebounds) );
double FT = Math.Round( (double)freeThrows / fieldGoalAttempts );
在前两个调用中,您都添加了一些内容。 0.5
和 0.44
都将值转换为双精度值,因为 0.5
和 0.44
都被视为双精度值。但是当你使用后两个时,它们都只使用整数,既不是双精度也不是十进制,并且可以转换为任何一种。要解决这个问题,您只需要做 Math.Round( (double) (*calculations*) );
或者,实际上更好的方法是将 一个 值转换为双精度 - 这样,它会以双精度计算除法。
(double)offensiveRebounds / (offensiveRebounds + opponentDefensiveRebounds)
(double)freeThrows / fieldGoalAttempts
在前两个中,您已将参数隐式转换为 Math.Round 并使用双精度(即 0.5 和 0.44)作为乘数。
后两个,你还没有。
int fieldGoals = int.Parse(Console.ReadLine());
int fieldGoalAttempts = int.Parse(Console.ReadLine());
int threePointFieldGoals = int.Parse(Console.ReadLine());
int turnovers = int.Parse(Console.ReadLine());
int offensiveRebounds = int.Parse(Console.ReadLine());
int opponentDefensiveRebounds = int.Parse(Console.ReadLine());
int freeThrows = int.Parse(Console.ReadLine());
int freeThrowAttempts = int.Parse(Console.ReadLine());
double eFG = Math.Round( (fieldGoals + 0.5 * threePointFieldGoals) / fieldGoalAttempts );
double TOV = Math.Round( turnovers / (fieldGoalAttempts + 0.44 * freeThrowAttempts + turnovers) );
double ORB = Math.Round( offensiveRebounds / (offensiveRebounds + opponentDefensiveRebounds) );
double FT = Math.Round( freeThrows / fieldGoalAttempts );
问题出在 double ORB
和 double FT
。
出于某种原因,我无法对它们使用 Math.Round
。它说:
the call is ambiguous between the following methods or properties : "Math.Round(double)" and "Math.Round(decimal)".
我只是不明白为什么前两个有效而后两个无效。
您正在使用 int
值调用 Math.Round
。您可能想先将它们转换为 double
:Math.Round( 1.0 * freeThrows...)
.
没有 Math.Round(int)
重载,但是 double
和 decimal
有重载,并且 int
可以隐式转换为两者。因此调用会产生歧义。
您尝试除以整数 - 结果将是整数。所以,你不能四舍五入这个数字。在除法和舍入之前将其转换为双精度:
double ORB = Math.Round( (double)offensiveRebounds / (offensiveRebounds + opponentDefensiveRebounds) );
double FT = Math.Round( (double)freeThrows / fieldGoalAttempts );
在前两个调用中,您都添加了一些内容。 0.5
和 0.44
都将值转换为双精度值,因为 0.5
和 0.44
都被视为双精度值。但是当你使用后两个时,它们都只使用整数,既不是双精度也不是十进制,并且可以转换为任何一种。要解决这个问题,您只需要做 Math.Round( (double) (*calculations*) );
或者,实际上更好的方法是将 一个 值转换为双精度 - 这样,它会以双精度计算除法。
(double)offensiveRebounds / (offensiveRebounds + opponentDefensiveRebounds)
(double)freeThrows / fieldGoalAttempts
在前两个中,您已将参数隐式转换为 Math.Round 并使用双精度(即 0.5 和 0.44)作为乘数。
后两个,你还没有。