Numbers to Words

This code converts given numbers to words.

For usage run (sample):

InitTextVariables;
numText := NumberToWords(1256,'');  // numText | Text | 100
Message(numText);
NumberToWords(number : Integer;appendScale : Text) : Text
numString := '';
IF number < 100 THEN
  IF number < 20 THEN
    numString := OnesText[number]
  ELSE BEGIN
    numString := TensText[number DIV 10];
    IF (number MOD 10) > 0 THEN
      numString := numString + ' ' + OnesText[number MOD 10];
  END
ELSE BEGIN
  pow := 0;
  powStr := '';
  IF number < 1000 THEN BEGIN // number is between 100 and 1000
    pow := 100;
    powStr := ThousText[1];
  END ELSE BEGIN // find the scale of the number
    log := number DIV 1000;
    pow := POWER(1000, log);
    powStr := ThousText[log + 1];
  END;

  numString := NumberToWords(number DIV pow, powStr) + ' ' + NumberToWords(number MOD pow,'');
END;

EXIT(numString + ' ' + appendScale);

// local variables
// numString	Text		
// pow	Integer		
// powStr	Text		
// log	Integer		

InitTextVariables()
OnesText[1] := 'one';
OnesText[2] := 'two';
OnesText[3] := 'three';
OnesText[4] := 'four';
OnesText[5] := 'five';
OnesText[6] := 'six';
OnesText[7] := 'seven';
OnesText[8] := 'eight';
OnesText[9] := 'nine';
OnesText[10] := 'ten';
OnesText[11] := 'eleven';
OnesText[12] := 'twelve';
OnesText[13] := 'thirteen';
OnesText[14] := 'fourteen';
OnesText[15] := 'fifteen';
OnesText[16] := 'sixteen';
OnesText[17] := 'seventeen';
OnesText[18] := 'eighteen';
OnesText[19] := 'nineteen';

TensText[1] := '';
TensText[2] := 'twenty';
TensText[3] := 'thirty';
TensText[4] := 'forty';
TensText[5] := 'fivty';
TensText[6] := 'sixty';
TensText[7] := 'seventy';
TensText[8] := 'eighty';
TensText[9] := 'ninty';

ThousText[1] := 'hundred';
ThousText[2] := 'thousand';
ThousText[3] := 'million';
ThousText[4] := 'billion';
ThousText[5] := 'trillion';

// global variables
// OnesText	Text	30 | Dim:20
// TensText	Text	30 | Dim:10
// ThousText	Text	30 | Dim:5
Advertisements

One thought on “Numbers to Words

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s