Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a C# System.Decimal remember trailing zeros?

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
}

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

like image 507
Robert Davey Avatar asked Jun 08 '10 11:06

Robert Davey


People also ask

What causes an AC to stop cooling?

The Filters Are Clogged Perhaps the most common cause of AC issues is clogged filters. Dirt, pet hair, pollen and dust can clog your filters. When filters get clogged, they begin to restrict the flow of air through your AC. The result is that the AC doesn't cool your indoor air effectively.

How does AC make air cold?

As the liquid refrigerant inside the evaporator coil converts to gas, heat from the indoor air is absorbed into the refrigerant, thus cooling the air as it passes over the coil. The indoor unit's blower fan then pumps the chilled air back through the home's ductwork out into the various living areas.

Why would AC stop working then starts again?

This issue is called short cycling. Short cycling forces the air conditioner to run cycles quickly, ending them before the home is properly cooled. When the air conditioner stops and starts problem happens over and over again, you lose energy efficiency.


1 Answers

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.

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

like image 137
Jon Skeet Avatar answered Oct 05 '22 09:10

Jon Skeet