Conversion of FLTP to CHAR type (ABAP)
Yesterday I had to convert a floating point variable (AUSP-AFLTV) to a character field.
For example: the value ‘6,250000000000000E+02’ had to be converted to ‘625 ‘.
A direct conversion to a CHAR or STRING type doesn’t do the job: the char or string field will contain the value ‘6.3E+02’.
I found several older posts on ARCHIVE.SAP.COM about this topic, for example https://archive.sap.com/discussions/thread/922677 but they referred to using function modules.
I found a easier way to get the correct result using nested CONV constructor expressions, but the archived posts cannot be changed anymore. So I would like to offer my solution that should work as of WAS740. I have included several options, where the option with DECFLOAT34 can have some unexpected results.
Using type I or P did the trick for me.
Using conversion to STRING and then to CHAR helped me with the left alignment of the value. Without the STRING conversion, the value would have been ‘ 625’.
DATA: l_decfloat TYPE decfloat34.
TYPES ty_p TYPE p DECIMALS 0 LENGTH 8.
SELECT * FROM ausp UP TO 10 ROWS INTO TABLE @DATA(ta_result) WHERE atflv <> '' .
LOOP AT ta_result ASSIGNING FIELD-SYMBOL(<result>).
DATA(l_character) = CONV char8( <result>-atflv ).
DATA(l_char_a) = CONV char8( CONV string( CONV ty_p( <result>-atflv ) ) ).
DATA(l_char_0) = CONV char8( CONV string( CONV i( <result>-atflv ) ) ).
l_decfloat = <result>-atflv.
DATA(l_char_1) = CONV char8( CONV string( l_decfloat ) ).
DATA(l_char_2) = CONV char8( CONV string( CONV decfloat34( <result>-atflv ) ) ).
WRITE: / <result>-atflv,
The Decfloat34 data type has an unexpected result in lines 3,4,8 and 9.
- If you need to convert a floating point variable (type F) to a character type, you can use the CONV expression, first converting it to a type I or P, then to STRING and/or a CHAR.
- You can nest the CONV expressions in one ABAP statement.
- Don’t use the built-in DECFLOAT types.