简体   繁体   English

Math.Round在AwayFromZero模式下失败

[英]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.

相关问题 JavaScript中的Math.round MidPointRounding.AwayFromZero - Math.round MidPointRounding.AwayFromZero in javascript 使用Math.Round和MidpointRounding.AwayFromZero四舍五入一个数字 - Round a number using Math.Round with MidpointRounding.AwayFromZero 与Delphi中的MidpointRounding.AwayFromZero相当的Math.Round()是什么? - What is the equivalent of Math.Round() with MidpointRounding.AwayFromZero in Delphi? Math.Round bug 81.725 MidpointRounding.AwayFromZero = 81.72 - Math.Round bug 81.725 MidpointRounding.AwayFromZero = 81.72 如何使用NET Math.Round( <decimal> , <int> ,MidpointRounding.AwayFromZero) - How to round off correctly with NET Math.Round(<decimal>,<int>,MidpointRounding.AwayFromZero) 使用Math.Round(ReportItems!category.Value,MidpointRounding.AwayFromZero)返回不正确的值 - Using Math.Round(ReportItems!category.Value,MidpointRounding.AwayFromZero) returns incorrect value .NET Math.Round(<double>,<int>,MidpointRounding.AwayFromZero)无法正常工作 - .NET Math.Round(<double>,<int>,MidpointRounding.AwayFromZero) not working correctly 为什么Math.Round(106.8,2,MidpointRounding.AwayFromZero)返回106.8而不是106.80? - Why does Math.Round(106.8, 2, MidpointRounding.AwayFromZero) return 106.8 and not 106.80? 文本框中的Math.Round - Math.Round in a textbox Math.Round加倍.5 - Math.Round double .5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM