为什么一个C#System.Decimal记得尾随零? [英] Why does a C# System.Decimal remember trailing zeros?

查看:93
本文介绍了为什么一个C#System.Decimal记得尾随零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个原因,一个C#System.Decimal记得尾随它与进入零的个数?
请看下面的例子:

Is there a reason that a C# System.Decimal remembers the number of trailing zeros it was entered with? See the following example:

public void DoSomething()
{
    decimal dec1 = 0.5M;
    decimal dec2 = 0.50M;
    Console.WriteLine(dec1);            //Output: 0.5
    Console.WriteLine(dec2);            //Output: 0.50
    Console.WriteLine(dec1 == dec2);    //Output: True
}



小数被归类为平等的,但的 DEC2 的回忆,这是一个额外的零输入。 ,这是什么?

The decimals are classed as equal, yet dec2 remembers that it was entered with an additional zero. What is the reason/purpose for this?

推荐答案

这可能是有用的,代表了许多的包括它的精度 - 所以0.5米可以用于表示0.45米和0.55米之间的任何(与适当的限制)和0.50米可以用于表示0.495米和0.545米之间的任何

It can be useful to represent a number including its accuracy - so 0.5m could be used to mean "anything between 0.45m and 0.55m" (with appropriate limits) and 0.50m could be used to mean "anything between 0.495m and 0.545m".

我怀疑的的开发商实际上并不使用此功能,但我可以看到它可能是有用的时候。

I suspect that most developers don't actually use this functionality, but I can see how it could be useful sometimes.

我相信这种能力初到.NET 1.1,顺便说一句 - 我认为在1.0小数总是有效的标准化。

I believe this ability first arrived in .NET 1.1, btw - I think decimals in 1.0 were always effectively normalized.

这篇关于为什么一个C#System.Decimal记得尾随零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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