# Board index » delphi » problem with money denomination

## problem with money denomination

Hi
I am writing a program that read in an amount of money under
a R100-00( the R stands for Rand, South African currency.) and
then calculates the least amount of denominations to issue.
The problem comes when I try to issue for example, 0.89 cents
The program will just issue 0.88 cents.

I am really stuck at this problem for 2 days. Can anybody
please assist me with solving this problem.
Attached is the code to have a look at
Thanks

program MoneyChange;
(* This programs reads in an amount of money and calculates the least value
of denominations in notes and coins to issue. *)

Uses crt;

Var money, count:real;

BEGIN
clrscr;
write ('Enter amount of money to evaluate ');
count := 0;
while money >= 50.00 do
begin
money := money - 50.00;
count := count + 1;
end;
writeln ('R50 note[s] : ', count:5:0);
count := 0;
while money >= 20.00 do
begin
money := money - 20.00;
count := count + 1;
end;
writeln ('R20 note[s] : ', count:5:0);
count := 0;
while money >= 10.00 do
begin
money := money - 10.00;
count := count + 1;
end;
writeln ('R10 note[s] : ', count:5:0);
count := 0;
while money >= 5.00 do
begin
money := money - 5.00;
count := count + 1;
end;
writeln ('R5 coin[s] :  ', count:5:0);
count := 0;
while money >= 2.00 do
begin
money := money - 2.00;
count := count + 1;
end;
writeln ('R2 coin[s] :  ', count:5:0);
count := 0;
while money >= 1.00 do
begin
money := money - 1.00;
count := count + 1;
end;
writeln ('R1 coin[s] :  ', count:5:0);
count := 0;
while money >= 0.50 do
begin
money := money - 0.50;
count := count + 1;
end;
writeln ('50c coin[s] : ', count:5:0);
count := 0;
while money >= 0.20 do
begin
money := money - 0.20;
count := count + 1;
end;
writeln ('20c coin[s] : ', count:5:0);
count := 0;
while money >= 0.10 do
begin
money := money - 0.10;
count := count + 1;
end;
writeln ('10c coin[s] : ', count:5:0);
count := 0;
while money >= 0.05 do
begin
money := money - 0.05;
count := count + 1;
end;
writeln ('5c coin[s] :  ', count:5:0);
count := 0;
while money >= 0.02 do
begin
money := money - 0.02;
count := count + 1;
end;
writeln ('2c coin[s] :  ', count:5:0);
count := 0;
while money >= 0.01 do
begin
money := money - 0.01;
count := count + 1;
end;
writeln ('1c coin[s] :  ', count:5:0);
END.

## Re:problem with money denomination

##### Quote
clint wrote:

> Hi
> I am writing a program that read in an amount of money under
> a R100-00( the R stands for Rand, South African currency.) and
> then calculates the least amount of denominations to issue.
> The problem comes when I try to issue for example, 0.89 cents
> The program will just issue 0.88 cents.

> I am really stuck at this problem for 2 days. Can anybody
> please assist me with solving this problem.
> Attached is the code to have a look at
> Thanks

> program MoneyChange;
> (* This programs reads in an amount of money and calculates the least value
> of denominations in notes and coins to issue. *)

> Uses crt;

> Var money, count:real;

> BEGIN
>   clrscr;
>   write ('Enter amount of money to evaluate ');
>   count := 0;
>   while money >= 50.00 do
>     begin
>        money := money - 50.00;
>        count := count + 1;
>     end;
>   writeln ('R50 note[s] : ', count:5:0);
>   count := 0;
>   while money >= 20.00 do
>     begin
>        money := money - 20.00;
>        count := count + 1;
>     end;
>   writeln ('R20 note[s] : ', count:5:0);
>   count := 0;
>   while money >= 10.00 do
>     begin
>        money := money - 10.00;
>        count := count + 1;
>     end;
>   writeln ('R10 note[s] : ', count:5:0);
>   count := 0;
>   while money >= 5.00 do
>     begin
>        money := money - 5.00;
>        count := count + 1;
>     end;
>   writeln ('R5 coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 2.00 do
>     begin
>        money := money - 2.00;
>        count := count + 1;
>     end;
>   writeln ('R2 coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 1.00 do
>     begin
>        money := money - 1.00;
>        count := count + 1;
>     end;
>   writeln ('R1 coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 0.50 do
>     begin
>        money := money - 0.50;
>        count := count + 1;
>     end;
>   writeln ('50c coin[s] : ', count:5:0);
>   count := 0;
>   while money >= 0.20 do
>      begin
>        money := money - 0.20;
>        count := count + 1;
>      end;
>   writeln ('20c coin[s] : ', count:5:0);
>   count := 0;
>   while money >= 0.10 do
>     begin
>        money := money - 0.10;
>        count := count + 1;
>     end;
>   writeln ('10c coin[s] : ', count:5:0);
>   count := 0;
>   while money >= 0.05 do
>     begin
>        money := money - 0.05;
>        count := count + 1;
>     end;
>   writeln ('5c coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 0.02 do
>      begin
>        money := money - 0.02;
>        count := count + 1;
>      end;
>   writeln ('2c coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 0.01 do
>      begin
>        money := money - 0.01;
>        count := count + 1;
>      end;
>    writeln ('1c coin[s] :  ', count:5:0);
> END.

Was it your intent to have no fraction on your printouts ?

--
Warning: poster of this email may be under the influence of
sleep inducing documents !

## Re:problem with money denomination

Im Artikel <3ebd4ec...@news1.mweb.co.za>, "clint" <gu...@nospam.mail.com>
schreibt:

##### Quote
>The problem comes when I try to issue for example, 0.89 cents
>The program will just issue 0.88 cents.

You mean 0.89 Rands?

This is a common problem with floating point numbers and arithmetic. Some
programming languages have an Currency data type, which allows for the correct
handling of numbers with a limited number of fractional digits.

You'll have less problems when you convert the amount into cents (or whatever
makes whole numbers) and use integer variables and arithmetic for all
calculations.

DoDi

## Re:problem with money denomination

##### Quote
"VBDis" <vb...@aol.com> wrote in message

news:20030511031104.04525.00000511@mb-m29.aol.com...

##### Quote
> Im Artikel <3ebd4ec...@news1.mweb.co.za>, "clint" <gu...@nospam.mail.com>
> schreibt:

> >The problem comes when I try to issue for example, 0.89 cents
> >The program will just issue 0.88 cents.

> You mean 0.89 Rands?

> This is a common problem with floating point numbers and arithmetic. Some
> programming languages have an Currency data type, which allows for the
correct
> handling of numbers with a limited number of fractional digits.

> You'll have less problems when you convert the amount into cents (or
whatever
> makes whole numbers) and use integer variables and arithmetic for all
> calculations.

> DoDi

Yes, I actually meant 0.89 Rands. I will try to convert the amounts into
cents.
and so how it goes. Thanks for the help.
Marlo
1st Year Comp Science student

## Re:problem with money denomination

JRS:  In article <3ebd4ec...@news1.mweb.co.za>, seen in
news:comp.lang.pascal.borland, clint <gu...@nospam.mail.com> posted at
Sat, 10 May 2003 21:11:44 :-

##### Quote
>I am writing a program that read in an amount of money under
>a R100-00( the R stands for Rand, South African currency.) and
>then calculates the least amount of denominations to issue.
>The problem comes when I try to issue for example, 0.89 cents
>The program will just issue 0.88 cents.
>Var money, count:real;

Type real is inexact, like all floats.  In TP>3 / BP, it is deprecated,
too. being slow; use double instead, normally.  See via <URL:http://www.
merlyn.demon.co.uk/pascal.htm>.  This might be a candidate for mini-FAQ
sec 3 or 3.1 (FAQ 3, before 3.1, should maybe be 3.0?).  AFAICS, it's
not really in TSFAQP, though #133, #143 are related.  Web/news FAQ
http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm #3 deals
with it, briefly; permission to copy should be available.

Change, however, to working in cents, with variables of type integer or
word or longint.

You should be able to combine all of those one-denomination loops into a
looped loop.

The problem is a subset of that in jad_9514.pas.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt>   RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

## Re:problem with money denomination

##### Quote

> Yes, I actually meant 0.89 Rands. I will try to convert the amounts into
> cents.
> and so how it goes. Thanks for the help.
> Marlo
> 1st Year Comp Science student

Okay, then I'll provide the explanation of why this happes in short
terms to you:

By using the type REAL or any floating point datatype (like EXTENDED)
you only have a limited number of bits which are stored in some exponent
notation. For example a 32 bit floating point number could be stored
like this:

1  Bit sign (+/-)
23 Bit mantissa (something like the value of the number)
8  Bit expnent based on 2, means two^this exponent

The exponent somehow determines where you have to think the comma in the
mantissa. The values after the comma are fractions added up, so there
always is some space between two neighbouring numbers which cannot be
covered by this sheme so the next possible number (the one whith the
minimum failure) is stored.

I'm no mathematician, I'm only student of some sort of computer science
myself, but I'm short before diploma ;-) So please forgive sligh
inaccurancied - I think at least the shema is correct.

Greetings

Markus