Tuesday, May 3, 2011

How to convert number to words


*&---------------------------------------------------------------------*
*& Report  ZCONVERTNUMBERTOWORDS
*&
*&---------------------------------------------------------------------*
*& Convert number to words
*&
*&---------------------------------------------------------------------*

REPORT  ZCONVERTNUMBERTOWORDS.


PARAMETERS: num TYPE i.

Data amt_in_num TYPE i.
Data words TYPE String.
Data final TYPE integer.
Data amt_in_words TYPE integer.

amt_in_num = num.

PERFORM convertIntoWords.
write:  words.


Form convertIntoWords.
  data: maxno type p.
  maxno = 10 ** 9.
  if ( amt_in_num >= maxno ).
    raise data_type_mismatch.
  endif.
*data declaration-------------------------------------------------*
  data: ten(10),single(6),final(130),dec(20),res type i,rp(7).
  data: a1 type i,a2 type i,str(20),d type p,m type i,wrdrep(20).
  data: cntr type i,f1 type i,f2 type i,f3 type i,f4 type i,f5 type i.
  data: f6 type i,f7 type i,f8 type i,f9 type i.

  d = ( amt_in_num * 100 ) div 100.
  res = ( amt_in_num * 100 ) mod 100.

  f1 = res div 10.
  f2 = res mod 10.
  perform setnum using f1 f2 changing wrdrep.
  f1 = 0. f2 = 0.
  dec = wrdrep.
  cntr = 1.
*Go in a loop dividing the numbers by 10 and store the
*residues as a digit in f1 .... f9
  while ( d > 0 ).
    m = d mod 10.
    d = d div 10.
    case cntr.
      when 1. f1 = m.
      when 2. f2 = m.
      when 3. f3 = m.
      when 4. f4 = m.
      when 5. f5 = m.
      when 6. f6 = m.
      when 7. f7 = m.
      when 8. f8 = m.
      when 9. f9 = m.
    endcase.
    cntr = cntr + 1.
  endwhile.
  cntr = cntr - 1.
*Going in loop and sending pair of digits to function setnum to get
*the standing value of digits in words
  while ( cntr > 0 ).

    if ( cntr <= 2 ).
      perform setnum using f2 f1 changing wrdrep.
      concatenate final wrdrep into final separated by ' '.
    elseif ( cntr = 3 ).
      if ( f3 <> 0 ).
        perform setnum using 0 f3 changing wrdrep.
        concatenate final wrdrep 'HUNDRED' into final separated by ' '.
      endif.
    elseif ( cntr <= 5 ).
      if ( f5 <> 0 ) or ( f4 <> 0 ).
        perform setnum using f5 f4 changing wrdrep.
        concatenate final wrdrep 'THOUSAND' into final separated by ' '
  .
      endif.
      if ( cntr = 4 ).
        cntr = 5.
      endif.
    elseif ( cntr <= 7 ).
      if ( f7 <> 0 ) or ( f6 <> 0 ).
        perform setnum using f7 f6 changing wrdrep.
        concatenate final wrdrep 'LAKH' into final separated by ' ' .
      endif.
    elseif ( cntr <= 9 ).
      perform setnum using f9 f8 changing wrdrep.
      concatenate final wrdrep 'CRORE' into final separated by ' ' .
    endif.

    cntr = cntr - 2.
  endwhile.
*Output the final
  if ( final = ' ONE' ).rp = 'Taka'(003).else. rp = 'Taka'(001).endif
.
  if ( final = '' ) and ( dec = '' ).
    final = 'NIL'.
  elseif ( final = '' ).
    concatenate dec 'Paise'(002) into final separated by ' ' .
  elseif ( dec = '' ).

    concatenate final rp into final separated by ' ' .
  else.
    concatenate final rp dec 'Paise'(002) into final separated by ' ' .
  endif.

words = final.

endform.



*&---------------------------------------------------------------------*
*&      Form  SETNUM
*&---------------------------------------------------------------------*
*       converts a number into words                                   *
*----------------------------------------------------------------------*
*  -->  a1,a2     two digits for 2nd and 1st place
*  <--  str       outpur in words
*----------------------------------------------------------------------*
data: ten(10),single(6),str(20).
*
form setnum using a1 a2 changing str.
  ten = ''.single = ''.
  if ( a1 = 1 ).

    case a2.
      when 0. ten = 'TEN'.
      when 1. ten = 'ELEVEN'.
      when 2. ten = 'TWELVE'.
      when 3. ten = 'THIRTEEN'.
      when 4. ten = 'FOURTEEN'.
      when 5. ten = 'FIFTEEN'.
      when 6. ten = 'SIXTEEN'.
      when 7. ten = 'SEVENTEEN'.
      when 8. ten = 'EIGHTEEN'.
      when 9. ten = 'NINETEEN'.
    endcase.
  else.

    case a2.
      when 1. single = 'ONE'.
      when 2. single = 'TWO'.
      when 3. single = 'THREE'.
      when 4. single = 'FOUR'.
      when 5. single = 'FIVE'.
      when 6. single = 'SIX'.
      when 7. single = 'SEVEN'.
      when 8. single = 'EIGHT'.
      when 9. single = 'NINE'.
    endcase.
    case a1.
      when 2. ten = 'TWENTY'.
      when 3. ten = 'THIRTY'.
      when 4. ten = 'FORTY'.
      when 5. ten = 'FIFTY'.
      when 6. ten = 'SIXTY'.
      when 7. ten = 'SEVENTY'.
      when 8. ten = 'EIGHTY'.
      when 9. ten = 'NINETY'.
    endcase.

  endif.
  if ( single <> '' ) and ( ten <> '' ).
    concatenate ten single into str separated by ' '.
  elseif single = ''.
    str = ten.
  else.
    str = single.
  endif.

endform.                              

1 comment:

  1. How to Add String Variable to Long Text Field?

    ReplyDelete