我在使用 C# 进行基本乘法和除法时遇到了问题。
它为 ((150/336) * 460) 返回 0,但答案应该是 205.357142857。
我推测这是因为 (150/336) 是一个小数,C# 将其四舍五入为 0。
如何在考虑所有小数位的情况下正确计算?
最佳答案
不,这是因为 150/336 是整数除法,它总是截断小数部分,因为结果也是 int
。
因此两者之一必须是十进制数:
double d = 150d / 336;
The division rounds the result towards zero, and the absolute value of the result is the largest possible integer that is less than the absolute value of the quotient of the two operands. The result is zero or positive when the two operands have the same sign and zero or negative when the two operands have opposite signs.
关于带小数位的 C# 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18243651/