Entering content frame

Obsolete Casting Locate the document in its SAP Library structure

CautionThe following additions to the ASSIGNstatement are obsolete. You should therefore no longer use them. They have been replaced by the CASTINGaddition.

 

Cast with built-in data types

To set the data type of a field symbol independently of the assigned object type (prior to the introduction of the CASTINGaddition was introduced), it was necessary to use the TYPEaddition directly in the ASSIGN statement:

ASSIGN ... TO <fs> TYPE dtype.

For dtype, however, you could only use the fixed-length elementary ABAP types (c, d, f, i, n, p, t, x), ‘s’ for two-byte integers (with sign) and ‘b’ for one byte integers (without sign) as literals or variables. With the CASTINGaddition, you can now declare any data type after the TYPEaddition.

There are two different situations where you could use a TYPEaddition:

        Typed field symbols

You could use the TYPE addition with a typed field symbol whenever the assigned data object type was incompatible with the field symbol type, but you needed to avoid triggering an error message. In this case, the specified type dtype must be compatible with the field symbol type. The field symbol then retains its data type, regardless of the assigned data object. You should now use casting with an implicit type declaration in this situation.

        Untyped field symbols

If you use the TYPE addition, an untyped field symbol <fs> adopt the data type specified in dtype instead of the data type and output length of the data object assigned to it. If the field symbol is used in a program after a statement with the TYPE addition, the content of the data object is interpreted in the same ways as for a field of type dtype. You should now use casting with an explicit type declaration in this situation.

Runtime errors occur if the declared data type is unknown; if the length of the declared data type does not match the type of the assigned field and if the alignments are incompatible.

Example

Example using the obsolete TYPE addition:

DATA txt(8) VALUE '19980606'.

DATA mytype(1) VALUE 'X'.

FIELD-SYMBOLS <fs>.

ASSIGN txt TO <fs>.
WRITE / <fs>.

ASSIGN txt TO <fs> TYPE 'D'.
WRITE / <fs>.

ASSIGN txt TO <fs> TYPE MYTYPE.
WRITE / <fs>.

Example using the CASTING addition:

DATA txt(8) VALUE '19980606'.

DATA mytype(1) VALUE 'X'.

FIELD-SYMBOLS <fs>.

ASSIGN txt TO <fs>.
WRITE / <fs>.

ASSIGN txt TO <fs> CASTING TYPE d.
WRITE / <fs>.

ASSIGN txt TO <fs> CASTING TYPE (mytype).
WRITE / <fs>.

In both cases, the system displays the following:

19980606

06061998

3139393830363036

In these examples, the string txt is assigned to the field symbol <fs> three times: once without casting, once cast to type d, and once cast to type x. The format of the second line depends on the settings in the user master record. The numbers in the last line are the hexadecimal codes of the characters in txt. They are platform-specific (in this case, ASCII).

Casting decimal places

To specify the number of decimal places in a field symbol when assigning it to a packed number of type p (before the CASTING statement was introduced), you needed to use the DECIMALS addition in the ASSIGNstatement:

ASSIGN ... TO <fs> DECIMALS dec.

You can use the DECIMALS addition in any variant of the ASSIGNstatement where the data object assigned to the field symbol has type p. In general, using the DECIMALS addition produces different numerical values for the field symbol and the assigned field. You can specify the number of decimal places dec as a literal or a variable. If dec  is not between 0 and 14, or the data object assigned to the field symbol is not a type p field, a runtime error occurs.

Example

Example using the obsolete DECIMALS addition:

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.

WRITE: / 'PACK1', pack1.

ASSIGN pack1 TO <f1> DECIMALS 1.
WRITE: / '<F1> ', <f1>.

pack2 = <f1>.
WRITE: / 'PACK2', pack2.

ASSIGN pack2 TO <f2> DECIMALS 4.
WRITE: / '<F2> ', <f2>.

pack3 = <f1> + <f2>.
WRITE: / 'PACK3', pack3.

<f2> = '1234.56789'.
WRITE: / '<F2> ', <f2>.
WRITE: / 'PACK2', pack2.

Example using the CASTING addition:

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.

WRITE: / 'PACK1', pack1.

ASSIGN pack1 TO <f1> CASTING TYPE p DECIMALS 1.
WRITE: / '<F1> ', <f1>.

pack2 = <f1>.
WRITE: / 'PACK2', pack2.

ASSIGN pack2 TO <f2> CASTING TYPE p DECIMALS 4.
WRITE: / '<F2> ', <f2>.

pack3 = <f1> + <f2>.
WRITE: / 'PACK3', pack3.

<f2> = '1234.56789'.
WRITE: / '<F2> ', <f2>.
WRITE: / 'PACK2', pack2.

In both cases, the list appears as follows:

PACK1          400,00

<F1>          4.000,0

PACK2        4,000,00

<F2>          40,0000

PACK3        4.040,00

<F2>       1.234,5679

PACK2      123.456,79

Each of the three type p fields has two decimal places. The field symbols <f1> and <f2> have one and four decimal places respectively. Note that the numeric values are different for the field symbols and for their assigned fields.

 

Leaving content frame