Character to Currency
format conversion
(No Function Module)
(No Function Module)
I crossed across a requirement to convert amount in character type
to currency format. The below code will convert the character type amount to
formatted currency value.
Source Code :
REPORT ZCHAR2CURR.
parameters char(30) type c.
data : txt(30) type c,
curr(40) type c,
len type i,
temp type i.
len = strlen( char ).
temp = len - 2.
concatenate ',' char+temp(2) into txt.
curr = txt.
char = char+0(temp).
len = strlen( char ).
do.
if len gt 3.
temp = len - 3.
concatenate ',' char+temp(3) into txt.
concatenate txt curr into curr.
char = char+0(temp).
len = strlen( char ).
else.
txt = char+0(len).
concatenate txt curr into curr.
exit.
endif.
enddo.
concatenate curr '.00' into curr.
write : 'Amount in Currency format :', curr.
parameters char(30) type c.
data : txt(30) type c,
curr(40) type c,
len type i,
temp type i.
len = strlen( char ).
temp = len - 2.
concatenate ',' char+temp(2) into txt.
curr = txt.
char = char+0(temp).
len = strlen( char ).
do.
if len gt 3.
temp = len - 3.
concatenate ',' char+temp(3) into txt.
concatenate txt curr into curr.
char = char+0(temp).
len = strlen( char ).
else.
txt = char+0(len).
concatenate txt curr into curr.
exit.
endif.
enddo.
concatenate curr '.00' into curr.
write : 'Amount in Currency format :', curr.
OUTPUT :
check out http://www.sapdev.co.uk/country/country_inttoext.htm for simple way of converting currency value to display format with correct formating!
ReplyDeleteWhat if char = 1? Then len is equal 1 and temp will be -1 and you will get a dump on line
ReplyDeleteconcatenate ',' char+temp(2) into txt.
What if char = 1? Then len is equal 1 and temp will be -1 and you will get a dump on line
ReplyDeleteconcatenate ',' char+temp(2) into txt.
its not working for 113.34.......junk program
ReplyDeletePARAMETERS char(30) TYPE c.
ReplyDeleteDATA : txt(30) TYPE c,
curr(40) TYPE c,
len TYPE i,
temp TYPE i,
char_2 TYPE char2.
SPLIT char AT '.' INTO char char_2.
len = strlen( char ).
IF len > 3.
temp = len - 3.
CONCATENATE ',' char+temp(3) INTO txt.
curr = txt.
char = char+0(temp).
len = strlen( char ).
DO.
IF len GT 3.
temp = len - 3.
CONCATENATE ',' char+temp(3) INTO txt.
CONCATENATE txt curr INTO curr.
char = char+0(temp).
len = strlen( char ).
ELSE.
txt = char+0(len).
CONCATENATE txt curr INTO curr.
EXIT.
ENDIF.
ENDDO.
IF char_2 IS INITIAL.
CONCATENATE curr '.00' INTO curr.
ELSE.
CONCATENATE curr '.' char_2 INTO curr.
ENDIF.
ELSE.
IF char_2 IS INITIAL.
CONCATENATE char '.00' INTO curr.
ELSE.
CONCATENATE char '.' char_2 INTO curr.
ENDIF.
ENDIF.
WRITE : 'Amount in Currency format :', curr.