Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Character String and Byte String Processing →  Statements for Character String and Byte String Processing →  WRITE - TO →  WRITE - format_options 

Decimal Floating Point Numbers, Formatting with STYLE

This example demonstrates the formatting of decimal floating point numbers.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: number  TYPE decfloat34,
          result1 TYPE c LENGTH 20,
          result2 TYPE c LENGTH 20,
          result3 TYPE c LENGTH 20,
          off     TYPE i,
          exc     TYPE REF TO cx_sy_unknown_currency.
    get_formats( ).
    number = '-12345.67890'.
    WRITE: /(32) 'Style', (20) 'WRITE TO',
            (20) 'Template (Raw)', (20) 'Template (User)'.
    ULINE.
    LOOP AT demo=>formats INTO demo=>format.
      WRITE number TO result1
        STYLE demo=>format-value LEFT-JUSTIFIED.
      result2 =
        |{ number STYLE  = (demo=>format-value) }|.
      result3 =
        |{ number STYLE  = (demo=>format-value)
                  NUMBER = USER }|.
      WRITE: /(32) demo=>format-name+2, result1, result2, result3.
    ENDLOOP.

Description

The program uses a classic list to depict the effects of all possible output formats from the CL_ABAP_FORMAT class for a decimal floating point number. The possible ouput formats are read using RTTI and are sorted according to their value. Formatting takes place once with the STYLE addition of the WRITE TO statement and twice with the STYLE parameter in embedded expressions of string templates.

For the first string template, the format RAW is implicitly used for the formating option NUMBER, which is why a point (.) and not a thousand separator is displayed as the decimal separator. Explicitly specifying the format USER creates the same formatting as for WRITE TO.