Kbase 21481: How to convert numbers to text strings
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  3/16/2007 |
|
Status: Unverified
GOAL:
How to convert numeric values to word representation
GOAL:
How to display a number as a word
FIX:
Sometimes it is necessary to display numeric values in their text equivalent value, for example displaying 2 as "TWO", or 3005 as "three thousand and five".
The following is code is a basic example on how to accomplish this.
FUNCTION numberToString RETURNS CHAR (INPUT number AS INTEGER) :
DEF VAR cad AS CHAR.
DEF VAR units AS CHAR EXTENT 9 INIT ["ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"].
DEF VAR teens AS CHAR EXTENT 9 INIT ["ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIVETEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"].
DEF VAR decens AS CHAR EXTENT 9 INIT ["TEN","TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"].
DEF VAR cents AS CHAR INIT "HUNDRED".
DEF VAR miles AS CHAR INIT "THOUSAND".
DEF VAR resu AS INTEGER.
DEF VAR i AS INTEGER.
IF number >= 1000 THEN DO:
ASSIGN resu = TRUNC (number / 1000,0)
number = number MOD 1000
cad = cad + numberToString(resu) + " " + miles + " ".
END.
IF number >= 100 THEN DO:
ASSIGN resu = TRUNC(number / 100,0)
number = number MOD 100
cad = cad + numberToString(resu) + " " + cents + " ".
END.
IF number > 10 AND number = 10 THEN DO:
ASSIGN resu = TRUNC(number / 10 , 0)
number = number MOD 10
cad = cad + decens[resu] + " ".
END.
IF number >= 1 THEN DO:
cad= cad + units[number].
END.
RETURN cad.
END.
/* example call */
DEF VAR cad1 AS CHAR.
cad1 = numberToString (55555). /* Any number from 1 to 999999 */
MESSAGE cad1 VIEW-AS ALERT-BOX.