带小数位的 C# 计算

标签 c#

我在使用 C# 进行基本乘法和除法时遇到了问题。

它为 ((150/336) * 460) 返回 0,但答案应该是 205.357142857。

我推测这是因为 (150/336) 是一个小数,C# 将其四舍五入为 0。

如何在考虑所有小数位的情况下正确计算?

最佳答案

不,这是因为 150/336 是整数除法,它总是截断小数部分,因为结果也是 int

因此两者之一必须是十进制数:

double d = 150d / 336;

参见:7.7.2 Division operator

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/

相关文章:

c# - 同步执行 WebSocket SendAsync()

c# - AsyncTimeout Action 过滤器有什么意义?

c# - 在业务层使用Server.MapPath

c# - 语音聊天,延迟增加(缓冲区) - 可以解决吗?

c# - C# 中是否可能进行部分泛型类型推断?

c# - 如何将 yahoo 登录集成到我的应用程序中?

c# - Unity 从资源中加载文本

c# - 在 python 中编码时在 c# 中解码 base64

c# - 有更好的方法来处理用户输入验证吗?

c# - 如何将gridview与来自两个不同表的数据绑定(bind)C#