I have this switch statement that decreases the double itemCost (originally 0.80) by 0.20 each time. When it reaches 0.00 i want to execute some code. It works fine until it gets to 0.00 but instead of being 0.00 itemCost becomes 5.55111512312578E-17.
Output values for itemCost as I press the button which executes switch statement are:
0.60, 0.40, 0.20, 5.55111512312578E-17, -0.20, -0.40 etc
Code:
switch (codeString)
{
case "20":
{
userAmount = userAmount + 0.20;
itemCost = itemCost - 0.20;
Console.WriteLine("" + itemCost);
if (itemCost == 0.00)
{
giveChange();
labelInstructions.Text = "giveChange";
}
else
{
string tempString = string.Format("{0:N2}", itemCost);
labelInstructions.Text = "Please insert £" + tempString;
}
break;
}
}
Anyone know why this weird behavior occurs and how to fix it?