The program doesn't calculate/display the correct calculation/number correctly
I'm trying to learn some C# for Unity game development, and tried out some random math stuff, but something seems to not work and I can't figure out why.
Console.WriteLine("What is the total amount you'd like change for? For example: 41,15");
double change = Convert.ToDouble(Console.ReadLine());
// 500 200 100 50 20 10 5
// 2 1 0,50 0,20 0,10 0,05
int fivehundred = 0, twohundred = 0, onehundred = 0, fifty = 0, twenty = 0, ten = 0, five = 0;
int ctwo = 0, cone = 0, cfifty = 0, ctwenty = 0, cten = 0, cfive = 0;
for (int i = 0; change >= 500; i++)
{
change -= 500;
fivehundred++;
}
for (int i = 0; change >= 200; i++)
{
change -= 200;
twohundred++;
}
for (int i = 0; change >= 100; i++)
{
change -= 100;
onehundred++;
}
for (int i = 0; change >= 50; i++)
{
change -= 50;
fifty++;
}
for (int i = 0; change >= 20; i++)
{
change -= 20;
twenty++;
}
for (int i = 0; change >= 10; i++)
{
change -= 10;
ten++;
}
for (int i = 0; change >= 5; i++)
{
change -= 5;
five++;
}
for (int i = 0; change >= 2; i++)
{
change -= 2;
ctwo++;
}
for (int i = 0; change >= 1; i++)
{
change -= 1;
cone++;
}
for (int i = 0; change >= 0.50; i++)
{
change -= 0.50;
cfifty++;
}
for (int i = 0; change >= 0.20; i++)
{
change -= 0.20;
ctwenty++;
}
for (int i = 0; change >= 0.10; i++)
{
change -= 0.10;
cten++;
}
for (int i = 0; change >= 0.05; i++)
{
change -= 0.05;
cfive++;
}
Console.WriteLine("500x {0}, 200x {1}, 100x {2}, 50x {3}, 20x {4}, 10x {5}, 5x {6}, 2x {7}, 1x {8}, 0,50x {9}, 0,20x {10}, 0,10x {11}, 0,05x {12}", fivehundred, twohundred, onehundred, fifty, twenty, ten, five, ctwo, cone, cfifty, ctwenty, cten, cfive);
Even though there's still 5 cents left, the result gives me is this: (this is when I entered 0,15 cents)
What is the total amount you'd like change for? For example: 41,15
0,15
500x 0, 200x 0, 100x 0, 50x 0, 20x 0, 10x 0, 5x 0, 2x 0, 1x 0, 0,50x 0, 0,20x 0, 0,10x 1, 0,05x 0 Press any key to continue . . .
If it's €0,09 or below, it does display that it needs 0,05 1x, but with anything above it with a remaining 5 cents, it doesn't. Everything else works as intended so far though.
(Also, any tips how I can make the code more efficient?)