Python code to express a large integer in words?
ptah
ptah at mjs400.co.uk
Tue Jul 31 13:41:35 EDT 2001
For interest I have included code below from the as400network Club Tech
iSeries Programming Tips - 06.21.01 it is written in IBM RPG IV and is a
kind of DLL (we call them service programs) that does the above, I would
like to see how much more succinct a Python equivalent could be, purely
because I would like to jump ship and code in something more expressive
and need to put the case for Python to people who are not in the habit
of thinking in an abstract way ;)
Any help greatly appreciated (I can't offer money, sorry).
Regards
Peter Moore
MJS400 (UK) Ltd
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 -
* -------------------------------------------------------------
C Select
C When Nbr = *Zero
C Eval RtnWords = 'zero'
C Other
C If Nbr < *Zero
C Eval RtnWords = 'negative'
C Eval Nbr = Nbr * -1
C EndIf
C Move Nbr AlphaNbr
C Do MaxGrps Count
C Eval Pos = ( Count * 3 ) - 2
C Eval Work3 = %Subst( AlphaNbr :
C Pos :
C 3 )
C If Work3 <> '000'
C If Work1 <> '0'
C Clear Idx
C Move Work1 Idx
C Eval RtnWords = %TrimR( RtnWords ) +
C ' ' +
C %TrimR( Words( Idx ) ) +
C ' hundred'
C EndIf
C If Work2 <> '00'
C Clear Idx
C Move Work2 Idx
C Eval RtnWords = %TrimR( RtnWords ) +
C ' ' +
C %TrimR( Words( Idx ) )
C EndIf
C Eval RtnWords = %TrimR( RtnWords ) +
C ' ' +
C %TrimR( Grps( Count ) )
C EndIf
C EndDo
C EndSl
C Eval RtnWords = %Trim( RtnWords )
C Return RtnWords
P CvtNbrToWords E
** CtData Words
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
**..SNIPPED..**
ninety-nine
** CtData Grps
trillion
billion
million
thousand
More information about the Python-list
mailing list