[英]Math.Round fail on AwayFromZero mode
Could someone tell me what is going wrong with following piece of code? 有人可以告诉我以下代码出了什么问题吗? It returns 0.14 instead of 0.15
它返回0.14而不是0.15
Math.Round(0.145, 2, MidpointRounding.AwayFromZero)
If the decimals are important use decimal datatype. 如果小数很重要,请使用小数数据类型。
There are books up and down on this subject and it's important to read up on this. 关于这一主题的书籍不计其数,因此请务必仔细阅读。 But the short version is this.
但是简短的版本是这个。
var a = Math.Round(0.145, 2, MidpointRounding.AwayFromZero);
//a == 0.14
var b = Math.Round(0.145m, 2, MidpointRounding.AwayFromZero);
//b == 0.15
So you just need to declare your variable as decimal. 因此,您只需要将变量声明为十进制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.