Skip to Content
Technical Articles
Author's profile photo B. Meijs

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,
l_character,
l_char_a,
l_char_0,
l_char_1,
l_char_2.
ENDLOOP.

Result:

The Decfloat34 data type has an unexpected result in lines 3,4,8 and 9.

Summary:

  • 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.

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jesús Antonio Santos Giraldo
      Jesús Antonio Santos Giraldo

      Interesting.

      Suppose(/Hope) DECFLOAT34 errors should be related to ABAP/Netweaver specific versions... otherwise, as you said is better to stay away from it.

      Author's profile photo B. Meijs
      B. Meijs
      Blog Post Author

      The DECFLOAT34 type conversions to f.e. 3E+1 are not errors (although they were unexpected by me). As far as I understand it, all values ending with 0 will be converted to values with exponentials. According to a colleague (and mathematician) this is perfectly normal behavior.

      So use type I or P (for fields with decimals).

      And if I say "Don't use the built-in DECFLOAT-types" I don't mean that you should never use them, only not in this data type conversion context.

      Author's profile photo Jesús Antonio Santos Giraldo
      Jesús Antonio Santos Giraldo

      Well, Glad to know.