Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Assignments →  Assigning References →  Setting Field Symbols →  ASSIGN →  ASSIGN - casting_spec →  Casting Examples 

Field Symbols - Casting Decimal Places

This example demonstrates how a casting of decimal places is carried out.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: pack1 TYPE p DECIMALS 2 VALUE '400',
          pack2 TYPE p DECIMALS 2,
          pack3 TYPE p DECIMALS 2.

    FIELD-SYMBOLS: <f1> TYPE ANY ,
                   <f2> TYPE ANY.

    DATA(out) = cl_demo_output=>new(
      )->begin_section( 'Cast of fractional places' ).

    out->write_data( pack1 ).

<span class="blue">* correct --------------------------------------------------------------</span>

    ASSIGN pack1 TO <f1> CASTING TYPE p DECIMALS 1.
    out->write( |<f1>: { <f1> }| ).

    pack2 = <f1>.
    out->write( |pack2: { pack2 }| ).

    ASSIGN pack2 TO <f2> CASTING TYPE p DECIMALS 4.
    out->write( |<f2>: { <f2> }| ).

    pack3 = <f1> + <f2>.
    out->write( |pack3: { pack3 }| ).

    <f2> = '1234.56789'.
    out->write( |<f2>: { <f2> }| ).
    out->write( |pack2: { pack2 }| ).

    out->display( ).

<span class="blue">* obsolete, not allowed in methods -------------------------------------</span>

    "ASSIGN pack1 TO <f1> DECIMALS 1.

    "pack2 = <f1>.

    "ASSIGN pack2 TO <f2> DECIMALS 4.

    "pack3 = <f1> + <f2>.

    "<f2> = '1234.56789'.

Description

Three fields of type p all have two decimal places. The field symbols <f1> and <f2> contain one and four decimal places respectively. The numerical values are different for the field symbols and the fields assigned to them.

The part of the method that has been turned into a comment also shows the syntax for the relevant obsolete casting.