C#十进制舍入不一致吗? [英] Is C# Decimal Rounding Inconsistent?

查看:61
本文介绍了C#十进制舍入不一致吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在与来自 SQL 小数 (38,30) 的 C# 中的小数精度作斗争,我终于做到了四舍五入的奇怪之处.我知道我可能忽略了这里显而易见的事情,但我需要一点洞察力.

我遇到的问题是 C# 不会产生我认为是一致的输出.

十进制a = 0.387518769125m;十进制 b = 0.3875187691250002636113061835m;Console.WriteLine(Math.Round(a, 11));Console.WriteLine(Math.Round(b, 11));Console.WriteLine(Math.Round(a, 11) == Math.Round(b, 11));

收益

0.387518769120.38751876913错误的

呃,0.38751876913?真的吗?我在这里错过了什么?

来自 MSDN:

<块引用>

如果小数位的数字是奇数,则改为偶数.否则,它保持不变.

为什么我看到的结果不一致?额外的精度不会改变小数位中的数字"...

解决方案

来自 MSDN:

<块引用>

如果decimals小数位右边的d中有一个非零数字并且其值为5,小数位的数字是奇数四舍五入,偶数不改变.如果 d 的小数位数少于 decimals,则 d 原样返回.

在你的第一种情况下

十进制a = 0.387518769125m;Console.WriteLine(Math.Round(a, 11));

在第 11 位的右侧有一位数那个数字是 5.因此,由于位置 11 是偶数,因此保持不变.因此,你得到

0.38751876912

第二种情况

十进制 b = 0.3875187691250002636113061835m;Console.WriteLine(Math.Round(b, 11));

第 11 位的右侧没有一位数.因此,这是小学四舍五入;如果下一个数字大于 4,则向上舍入,否则向下舍入.由于第 11 位右侧的数字大于 4(即 5),因此我们将四舍五入让您看到

0.38751876913

<块引用>

为什么我看到的结果不一致?

你不是.结果与文档完全一致.

I've been fighting decimal precision in C# coming from a SQL Decimal (38,30) and I've finally made it all the way to a rounding oddity. I know I'm probably overlooking the obvious here, but I need a little insight.

The problem I'm having is that C# doesn't produce what I would consider to be consistent output.

decimal a = 0.387518769125m;
decimal b = 0.3875187691250002636113061835m;

Console.WriteLine(Math.Round(a, 11));
Console.WriteLine(Math.Round(b, 11));
Console.WriteLine(Math.Round(a, 11) == Math.Round(b, 11));

Yields

0.38751876912
0.38751876913
False

Uhh, 0.38751876913? Really? What am I missing here?

From MSDN:

If the digit in the decimals position is odd, it is changed to an even digit. Otherwise, it is left unchanged.

Why am I seeing inconsistent results? The additional precision isn't changing the 'digit in the decimals position'...

解决方案

From MSDN:

If there is a single non-zero digit in d to the right of the decimals decimal position and its value is 5, the digit in the decimals position is rounded up if it is odd, or left unchanged if it is even. If d has fewer fractional digits than decimals, dis returned unchanged.

In your first case

decimal a = 0.387518769125m;
Console.WriteLine(Math.Round(a, 11));

there is a single digit to the right of the 11th place, and that number is 5. Therefore, since position 11 is even, it is left unchanged. Thus, you get

0.38751876912

In your second case

decimal b = 0.3875187691250002636113061835m;
Console.WriteLine(Math.Round(b, 11));

there is not a single digit to the right of the 11th place. Therefore, this is straight up grade-school rounding; you round up if the next digit is greater than 4, otherwise you round down. Since the digit to the right of the 11th place is more than 4 (it's a 5), we round up so you see

0.38751876913

Why am I seeing inconsistent results?

You're not. The results are completely consistent with the documentation.

这篇关于C#十进制舍入不一致吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆