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 VALUE '-12345.67890',
          BEGIN OF result,
            style         TYPE string,
            write_to      TYPE c length 20,
            template_raw  TYPE c length 20,
            template_user TYPE c length 20,
          END OF result,
          results LIKE TABLE OF result,
          off     TYPE i,
          exc     TYPE REF TO cx_sy_unknown_currency.
    get_formats( ).
    LOOP AT demo=>formats INTO demo=>format.
      result-style = demo=>format-name+2.
      WRITE number TO result-write_to
        STYLE demo=>format-value LEFT-JUSTIFIED.
      result-template_raw =
        |{ number STYLE  = (demo=>format-value) }|.
      result-template_user =
        |{ number STYLE  = (demo=>format-value)
                  NUMBER = USER }|.
      APPEND result TO results.
    ENDLOOP.
    cl_demo_output=>display( results ).

Description

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

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