未正确舍入
Math.Round(keyRate, 5).ToString("N5")
如果 keyRate 是像 3.7066666666 这样的数字,您将看到 3.70666 而不是 3.70667
这里可能有什么问题?
Math.Round(keyRate, 5).ToString("N5")
If keyRate is a number like 3.7066666666, you will see 3.70666 rather than 3.70667
What could be the issue here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Math.Round 函数默认使用所谓的银行四舍五入。银行家四舍五入到偶数。要获得更传统的舍入类型,请像这样调用 Round 方法:
更正:再次查看 @slandau 的号码后,我意识到银行家的舍入不是这里的问题。仅当所需精度右侧的数字的小数部分恰好位于两个值之间的中间时,才适用银行家四舍五入。换句话说,
无论哪种方式,以下(银行家四舍五入)
@slandau 的结果都应该是 3.70667。
The Math.Round function used what's called banker's rounding by default. Banker's rounding rounds toward the even number. To get the more traditional type of rounding, call the Round method like this:
correction: After looking again at @slandau's number I realized that banker's rounding isn't the issue here. Banker's rounding only applies when the fractional portion of the number to the right of the desired precision is exactly halfway between two values. In other words
while the following (Banker's rounding)
@slandau's result should be 3.70667 either way.
您应该使用小数和
Math.Round(keyRate, 5, MidpointRounding.AwayFromZero)
这不适用于 double
You should use decimal and
Math.Round(keyRate, 5, MidpointRounding.AwayFromZero)
It isn't work with double