Figure to Words for India but can be modified to any requirement

Content Author:  Jayanta Narayan Choudhuri
Author Email:      sss@cal.vsnl.net.in
Author Website:  http://www.geocities.com/ojnc
* Test Program
Report ZJNC3.

Data: OffSet      type I,
      Amt2Words   type String.


Selection-Screen: Begin of Block B1Opts with Frame Title TEXT-002.
    Parameters: MyAmount      type P  Decimals 2 ,
                MyChrsLn      type I             ,
                MyNumLns      type I             .
Selection-Screen: End of Block B1Opts.


CALL FUNCTION 'ZAMT2WORDS'
  EXPORTING
    AMOUNT         = MyAmount
    CHRSLINE       = MyChrsLn
    NUMLINES       = MyNumLns
  CHANGING
    AMT2WDS        = Amt2Words.


Move 0 to OffSet.
Do MyNumLns Times.
   Write: / Amt2Words+OffSet(MyChrsLn).
   Add MyChrsLn to OffSet.
EndDo.

------------------------------------------------------------------------------------------
* Function Module ZAMT2WORDS 

FUNCTION ZAMT2WORDS.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(AMOUNT) TYPE  P
*"     REFERENCE(CHRSLINE) TYPE  I
*"     REFERENCE(NUMLINES) TYPE  I
*"  CHANGING
*"     REFERENCE(AMT2WDS) TYPE  STRING
*"----------------------------------------------------------------------

* Nicely Cased words spelt corectly
* suits India
* takes care of singular & plural
* word wraps at line end
* ChrsLine = max no. of chars per line for wordwrapping.
* NumLines = max no. of lines

  Data: chramount   type String,
        workStr     type String,
        numAmount   type P  Decimals 2.

  Data: MyTwoDigits type I,
        quotient    type I,
        remainder   type I.


  Data: cr          type I,
        lk          type I,
        th          type I,
        hun         type I,
        ten         type I,
        paisa       type I.

  Compute MAXLEN = ChrsLine * NumLines.

  Move '' to mySpaces.
  Do MAXLEN Times.
       Concatenate mySpaces '.' into mySpaces.
  EndDo.

  Move ChrsLine to LineLength.

  If LineLength < 20 Or LineLength > 120.
    Amt2Words = 'Invalid Parameters'.
    Perform Rpad2MAX.
    Amt2Wds = Amt2Words.
    Return.
  EndIf.
  
  If Amount = 0.
     Amt2Words = 'Rupees Nil Only'.
     Perform Rpad2MAX.
     Amt2Wds = Amt2Words.
     Return.
  EndIf.  

* Reference is 'Seventeen'.

*     Units & Teens
  Words1 =
        'One......' &
        'Two......' &
        'Three....' &
        'Four.....' &
        'Five.....' &
        'Six......' &
        'Seven....' &
        'Eight....' &
        'Nine.....' &
        'Ten......' &
        'Eleven...' &
        'Twelve...' &
        'Thirteen.' &
        'Fourteen.' &
        'Fifteen..' &
        'Sixteen..' &
        'Seventeen' &
        'Eighteen.' &
        'Nineteen.'.

Move '' to Words2.

*     Tens
  Words2 =
        'Twenty...' &
        'Thirty...' &
        'Forty....' &
        'Fifty....' &
        'Sixty....' &
        'Seventy..' &
        'Eighty...' &
        'Ninety...'.

  If Amount < 0.
     Amt2Words = 'Rupees Minus'.
   Else.
     Amt2Words = 'Rupees'.
  EndIf.


*                      crlkthhtn.ps.
  Compute numAmount = 1000000000 + Abs( Amount ).
  Move numAmount to chramount.
  Move chramount+1(2) to cr.
  Move chramount+3(2) to lk.
  Move chramount+5(2) to th.
  Move chramount+7(1) to hun.
  Move chramount+8(2) to ten.
  Move chramount+11(2) to paisa.

  If cr > 0.
     Perform InWords2Digits Using cr.
     If cr > 1.
        Perform AppendWord Using 'Crores'.
      Else.
        Perform AppendWord Using 'Crore'.
     EndIf.
  EndIf.

  If lk > 0.
     Perform InWords2Digits Using lk.
     If lk > 1.
        Perform AppendWord Using 'Lakhs'.
      Else.
        Perform AppendWord Using 'Lakh'.
     EndIf.
  EndIf.

  If th > 0.
     Perform InWords2Digits Using th.
     Perform AppendWord Using 'Thousand'.
  EndIf.

  If hun > 0.
     Perform InWords2Digits Using hun.
     Perform AppendWord Using 'Hundred'.
  EndIf.

  If ten > 0.
     Perform InWords2Digits Using ten.
  EndIf.

  If paisa > 0.
     If Amt2Words = 'Rupees'.
        Amt2Words = 'Paisa'.
      Else.
        Perform AppendWord Using 'And'.
        Perform AppendWord Using 'Paisa'.
     EndIf.
     Perform InWords2Digits Using paisa.
  EndIf.

  Perform AppendWord Using 'Only'.

  Perform Rpad2MAX.

  Amt2Wds = Amt2Words.

ENDFUNCTION.
------------------------------------------------------------------------------------------
* TOP level Include of Function Group ZUTIL

FUNCTION-POOL ZUTIL.                        "MESSAGE-ID ..

* Global ZUTIL Data
Data: Words1      type String,
      Words2      type String,
      Amt2Words   type String,
      mySpaces    type String,
      LineLength  type I     ,
      MaxLen      type I     .
      
*&---------------------------------------------------------------------*
*&      Form  AppendWord
*&---------------------------------------------------------------------*
FORM AppendWord Using AddWord type String.

  Data: NowLen       type I,
        AddLen       type I,
        NewLen       type I,
        DifLen       type I,
        workStr      type String.

  NowLen = StrLen( Amt2Words ).
  NowLen = NowLen Mod LineLength.
  AddLen = StrLen( AddWord ).

  Compute NewLen = NowLen + 1 + AddLen.
  If NewLen > LineLength.
      DifLen = LineLength - NowLen.
      If DifLen > 1.
         Subtract 1 from DifLen.
         Move mySpaces+0(DifLen) to workStr.
         Concatenate Amt2Words workStr into Amt2Words.
      EndIf.
  EndIf.

* exact line boundary
  If NowLen = 0.
     Concatenate Amt2Words AddWord into Amt2Words.
  Else.
     Concatenate Amt2Words '.' AddWord into Amt2Words.
  EndIf.

EndForm.        " AppendWord

*&---------------------------------------------------------------------*
*&      Form  InWords2Digits
*&---------------------------------------------------------------------*
FORM InWords2Digits Using MyTwoDigits type I.
  Data: quotient    type I,
        remainder   type I,
        workStr     type String,
        workIdx     type I.

  If MyTwoDigits > 19 .
     Compute quotient = MyTwoDigits Div 10.
     Compute remainder = MyTwoDigits Mod 10.

     Compute workIdx = ( quotient - 2 ) * 9.
     Move Words2+workIdx(9) to workStr.
     Replace All Occurrences Of  '.'  in workStr with ' '.
     Perform AppendWord Using workStr.

     If remainder > 0 .
        Compute workIdx = ( remainder - 1 ) * 9.
        Move Words1+workIdx(9) to workStr.
        Replace All Occurrences Of  '.'  in workStr with ' '.
        Perform AppendWord Using workStr.
     EndIf.
   Else.
     Compute workIdx = ( MyTwoDigits - 1 ) * 9.
     Move Words1+workIdx(9) to workStr.
     Replace All Occurrences Of  '.'  in workStr with ' '.
     Perform AppendWord Using workStr.
  EndIf.

EndForm.        " InWords2Digits

*&---------------------------------------------------------------------*
*&      Form  Rpad2MAX
*&---------------------------------------------------------------------*
FORM Rpad2MAX.
    Concatenate Amt2Words mySpaces into Amt2Words.
    Move Amt2Words+0(MAXLEN) to Amt2Words.
    Concatenate Amt2Words '*' into Amt2Words.
    Replace All Occurrences Of  '.'  in Amt2Words With ' !'.
    Replace All Occurrences Of  '!'  in Amt2Words With ' '.
EndForm.        " Rpad2MAX
------------------------------------------------------------------------------------------

ABAP Tips

Related ABAP Topics:
Table CDHDR and CDPOS Usage

Get help for your ABAP problems
Do you have a ABAP Question?

SAP Books
SAP Certification, Interview Questions, Functional, Basis Administration and ABAP Programming Reference Books

More ABAP Tips

Main Index
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.