Rene Vucko
Rene Vucko

Reputation: 43

C# money splitting program

double a, ostanek;
a=Convert.ToDouble(Console.ReadLine()); 
int apoen_500, apoen_200, apoen_100, apoen_50, apoen_20, apoen_10, apoen_5, apoen2, apoen1;
int ost50cent, ost20cent, ost10cent, ost5cent, ost2cent, ost1cent;
apoen_500 = (int)a / 500;
ostanek = a%500;

apoen_200=(int)ostanek/200;
ostanek = ostanek % 200;

apoen_100 = (int)ostanek / 100;
ostanek = ostanek % 100;

apoen_50 = (int)ostanek / 50;
ostanek = ostanek % 50;
apoen_20 = (int)ostanek / 20;
ostanek = ostanek % 20;
apoen_10 = (int)ostanek / 10;
ostanek = ostanek % 10;
apoen_5 = (int)ostanek /5 ;
ostanek = ostanek % 5;
apoen2 = (int)ostanek / 2;
ostanek = ostanek % 2;
apoen1 = (int)ostanek / 1;
ostanek = ostanek % 1;

ost50cent = (int)(ostanek / 0.50);
ostanek = ostanek % 0.50;
ost20cent = (int)(ostanek / 0.20);
ostanek = ostanek % 0.20;
ost10cent = (int)(ostanek / 0.10);
ostanek = ostanek % 0.10;
ost5cent = (int)(ostanek / 0.05);
ostanek = ostanek % 0.05;
ost2cent = (int)(ostanek / 0.02);
ostanek = ostanek % 0.02;
ost1cent = (int)(ostanek / 0.01);
ostanek = ostanek % 0.01;

Console.WriteLine(apoen_500 +"x500");
Console.WriteLine(apoen_200 + "x200");
Console.WriteLine(apoen_100 + "x100");
Console.WriteLine(apoen_50 + "x50");
Console.WriteLine(apoen_20 + "x20");    
Console.WriteLine(apoen_10 + "x10");    
Console.WriteLine(apoen_5 + "x5");
Console.WriteLine(apoen2 + "x2");
Console.WriteLine(apoen1 + "x1");
Console.WriteLine(ost50cent + "x50 centov");
Console.WriteLine(ost20cent + "x20 centov");
Console.WriteLine(ost10cent + "x10 centov");
Console.WriteLine(ost5cent + "x5 centov");
Console.WriteLine(ost2cent + "x2 centov");
Console.WriteLine(ost1cent + "x1 centov");

So I have this code, but it doesn't work as planned. It's supossed to split money into smaller pieces, but it doesn't work for the 50, 20, 10 ,5 ,2 1 cents. It doesn't display correctly. Like if I enter 90,75, it displays 1x50€ 1x 20€ 1x50c 1x20c 2x 2 cents, but it should be 1x5 cents.

Upvotes: 0

Views: 1625

Answers (1)

Enigmativity
Enigmativity

Reputation: 117124

You should use decimal for currency calculations. double is for scientific calculations.

Change these lines:

decimal a, ostanek;
a = Convert.ToDecimal(Console.ReadLine());

and:

ost50cent = (int)(ostanek / 0.50m);
ostanek = ostanek % 0.50m;
ost20cent = (int)(ostanek / 0.20m);
ostanek = ostanek % 0.20m;
ost10cent = (int)(ostanek / 0.10m);
ostanek = ostanek % 0.10m;
ost5cent = (int)(ostanek / 0.05m);
ostanek = ostanek % 0.05m;
ost2cent = (int)(ostanek / 0.02m);
ostanek = ostanek % 0.02m;
ost1cent = (int)(ostanek / 0.01m);
ostanek = ostanek % 0.01m;

I then get this output when I enter 90.75:

0x500
0x200
0x100
1x50
2x20
0x10
0x5
0x2
0x1
1x50 centov
1x20 centov
0x10 centov
1x5 centov
0x2 centov
0x1 centov

Here's an alternative way to compute the result:

var denominations = new[]
{
    500m, 200m, 100m, 50m, 20m, 10m, 5m, 2m, 1m,
    0.5m, 0.2m, 0.1m, 0.05m, 0.02m, 0.01m
};

var amount = Convert.ToDecimal(Console.ReadLine());

var change = new List<decimal>();
var remaining = amount;
foreach (var denomination in denominations)
{
    while (remaining >= denomination)
    {
        remaining -= denomination;
        change.Add(denomination);
    }
}

var display =
    String.Join(
        Environment.NewLine,
        change
            .GroupBy(x => x)
            .Select(x => String.Format("{0}x{1}", x.Count(), x.Key)));

With the 90.75 input, I get this output:

1x50
2x20
1x0.5
1x0.2
1x0.05

Upvotes: 1

Related Questions