## Printing a Number as a Character

5 pts.
Tags:
Character
print
RPG
How do you print a numeric field as characters? For example on check print, how do you print Nine Hundred Ninety-Nine instead of 999?

Thanks. We'll let you know when a new response is added.

You write a procedure that looks at each digit and chooses the words that describe the digit in that position. Usually you will have a few arrays that will hold the various words such as “nine hundred”, “ninety” and “nine”.

So from your example value 999, your program would find that the first significant digit was the 9 in the hundreds position. It could then retrieve the ninth value from the array for hundreds. That value might be “nine hundred”. Your program moves that string to the output string.

The program then moves to the next position — the tens. It finds a 9, so it retrieves the ninth entry from the array for tens — “ninety”. It concatenates that to the value already in the output string to get a new value — “nine hundred ninety”.

Overall, your program simply goes through the digits and selects the appropriate strings for all of the parts. The strings get concatenated together.

You can work left-to-right and build the output string as you go along. Or you can go through the digits (in either direction) and build a list of words that you concatenate together after processing all of the digits. Or you can go through the digits (in either direction) and simply set all the array indexes — string the array elements together after finishing the digits. Or…

Well, there are lots of possible ways, probably as many ways as there are people who might read this item. The “right” way depends on how perfect the result must be according to some specified requirement, how efficient it needs to be, how important the precise details such as hyphenation are, whether decimal positions are involved… whether you’re coding in RPG II or Smalltalk or other language…

This is “introductory”-level concepts stuff. Are you learning programming? Lots of examples of coding are out on the internet.

Tom

## Discuss This Question: 3 Replies

Thanks. We'll let you know when a new response is added.
• Here is a Service Program that you can use, but as Tom wrote there are tons of examples out on the Internet.
```  // **************************************************************
//  =  Service program... NbrToWords                            =
//  =  Description....... Service program to convert a number   =
//  =                     to words                              =
//  =                                                           =
//  =  CrtRPGMod  Module( YourLib/NbrToWords )       +          =
//  =             SrcFile( YourLib/YourSrcFile )                =
//  =  CrtSrvPgm  SrvPgm( YourLib/NbrToWords )       +          =
//  =             Export( *All )                     +          =
//  =             ActGrp( *Caller )                             =
//  =============================================================
H NoMain

//  -------------------------------------------------------------
//  -  Procedure prototypes                                     -
//  -------------------------------------------------------------

D CvtNbrToWords   PR           200
D                               15  0 Value

//  -------------------------------------------------------------
//  -  Global variables                                         -
//  -------------------------------------------------------------

D MaxGrps         C                   5

D Words           S             13    Dim( 99 )
D                                     CtData

D Grps            S              8    Dim( MaxGrps )
D                                     CtData
//  =============================================================
//  =  Procedure:   CvtNbrToWords                               =
//  =  Description: Convert number to words                     =
//  =============================================================

P CvtNbrToWords   B                   Export

//  -------------------------------------------------------------
//  -  Procedure interface                                      -
//  -------------------------------------------------------------
D CvtNbrToWords   PI           200
D  Nbr                          15  0 Value

//  -------------------------------------------------------------
//  -  Variable declarations                                    -
//  -------------------------------------------------------------

D AlphaNbr        S             15

D WorkFld         DS
D  Work3                         3
D  Work2                         2    Overlay( Work3 : 2 )
D  Work1                         1    Overlay( Work3 : 1 )
D Count           S              5I 0
D Pos             S              5I 0
D Idx             S              5I 0

D RtnWords        S            200    Inz( *Blank )

//  -------------------------------------------------------------
//  -  Convert number to words                                  -
//  -------------------------------------------------------------

/Free
Select;
When Nbr = *Zero;
RtnWords = 'zero';

Other
If Nbr < *Zero;
RtnWords = 'negative';
Nbr = Nbr * -1;
EndIf;

AlphaNbr = Nbr;

Do MaxGrps Count;

Pos   = ( Count * 3 ) - 2;
Work3 = %Subst( AlphaNbr :
Pos      :
3        );
If Work3 <> '000';
If Work1 <> '0';
Clear Idx;
Idx = Work1;
RtnWords = %TrimR( RtnWords )     +
' '                    +
%TrimR( Words( Idx ) ) +
' hundred';
EndIf;

If Work2 <> '00';
Clear Idx;
Idx = Work2;
RtnWords = %TrimR( RtnWords )     +
' '                    +
%TrimR( Words( Idx ) );
EndIf;

RtnWords = %TrimR( RtnWords )    +
' '                   +
%TrimR( Grps( Count ) );

EndIf;

EndDo;
EndSl;

RtnWords = %Trim( RtnWords );

Return RtnWords;

P CvtNbrToWords   E
/End-free

** CtData Words
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
thirty-six
thirty-seven
thirty-eight
thirty-nine
forty
forty-one
forty-two
forty-three
forty-four
forty-five
forty-six
forty-seven
forty-eight
forty-nine
fifty
fifty-one
fifty-two
fifty-three
fifty-four
fifty-five
fifty-six
fifty-seven
fifty-eight
fifty-nine
sixty
sixty-one
sixty-two
sixty-three
sixty-four
sixty-five
sixty-six
sixty-seven
sixty-eight
sixty-nine
seventy
seventy-one
seventy-two
seventy-three
seventy-four
seventy-five
seventy-six
seventy-seven
seventy-eight
seventy-nine
eighty
eighty-one
eighty-two
eighty-three
eighty-four
eighty-five
eighty-six
eighty-seven
eighty-eight
eighty-nine
ninety
ninety-one
ninety-two
ninety-three
ninety-four
ninety-five
ninety-six
ninety-seven
ninety-eight
ninety-nine
** CtData Grps
trillion
billion
million
thousand
```