Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Money 2

Status
Not open for further replies.

filipe26

Programmer
Mar 17, 2003
152
PT
How can i transform for example this"10,00" in "ten Euros" and so on.Numeric in Full Words.
 
You need fairly large case statements!

Split up the amount into two variables called 'euros' and 'cents'.

Your code should look something like this (but you will have to add the extra code where I have put ...):
Code:
function FullWords ( euros, cents: integer ): string;
begin
  result := ConvertEuros(euros) + ConvertCents(cents);
end;

function ConvertCents(cents: integer): string;
begin
 case cents of 
  0: result := '';
  1: result := 'one';
  2: result := 'two';
  3: result := 'three';
  ...
  10: result := 'ten';
  11: result := 'eleven';
  ... 
  20..29: result := 'twenty' + ConvertUnits(cents mod 10);
  30..39: result := 'thirty' + ConvertUnits(cents mod 10);
  40..49: result := 'forty' + ConvertUnits(cents mod 10);
  ...
 end;
 if result <> '' then
  result := ' and ' + result + ' cents';
end;

function ConvertUnits(units: integer): string;
begin
 case units of
  0: result := '';
  1: result := 'one';
  2: result := 'two';
  ...
  9: result := 'nine';
 end;
end;

function ConvertEuros(euros: integer): string;
begin
 case euros of
  0: result := '';
  1: result := 'one';
  2: result := 'two';
  ...  // handle values up to 19 separately
  20..29: result := 'twenty' + ConvertUnits(euros mod 10);
  30..29: result := 'thirty' + ConvertUnits(euros mod 10);
  ...
  90..99: result := 'ninety' + ConvertUnits(euros mod 10);
  100..9999: result := ConvertUnits(euros div 100) + ' hundred and ' + ConvertEuros(euros mod 100);
 end;
end;
I haven't tested this code - it is here to show you how you should approach the problem. I have only coded up to 9999 euros but note the use of recursion in the ConvertEuros function. This is the secret of converting larger numbers. You would do something similar when handling thousands and millions. Of course, there are various ways of saying a number such as 1234. For example, you could say twelve hundred and thirty four or you could say one thousand two hundred and thirty four. Both would be correct but you need to make some design decisions.

Andrew

 
Andrew,
Did you just throw that together after seeing this posting or did you have that laying around from a project? If the former, VERY IMPRESSIVE!!! I wish I could code like that!!

Stars from a &quot;code groupie&quot;!





Leslie
landrews@metrocourt.state.nm.us

There are 10 types of people in the world -
those who understand binary
and
those who don't!
 
Well, I threw it together after I saw the posting. It seemed like an interesting problem. When I thought about using recursion as a solution I got excited enough to type the reply. When recursion is used appropriately in algorthims the result is often quite elegant.

Actually, I've noticed some minor problems in my suggested code. For example, at the end of the ConvertEuros function it should append ' euros' to result. Also, where the result is 'one' the word 'cents' should really be 'cent' (same for 'euros', of course).

I'm quite interested to know what system actually needs this function.


Andrew
 
Ok it results but how can i get only the decimals?

number=23,45

Euros:=int(23,45)

and cents?
 
I replied a bit too quick as you need cents as an integer. It should be more like
Code:
cents := Int ( ( 23.45 - euros ) * 100 );

Andrew
 
Off course im am ridiculous today.Thanks and Sorry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top