So, here comes a repeated question:
In one of my client's machine, i have this code,
public double getFreeSpace()
{
return this.totalSize - this.listElements.Sum();
}
and when subtraction like this happens: (1.0 - 0.8)
the expected result is (0.2)
but she gets the result some thing as 0.199999
.
While this is not the intended result, how to get 0.2
as output for this code?
The stackoverflow question here, discusses the same issue but suggests using decimal
as the alternative. (But, also note that in the same answer, we get a warning : It is a 128 bit datatype as opposed to 64 bit which is the size of a double
).
I also tried, Math.Round(1.0-0.8, 0, MidpointRounding.AwayFromZero)
but this does not do the job.
The question now is, how to get the desired answer as 0.2
while still keeping the same datatype as double
without using a decimal
?