Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Built-In Types, Data Objects, Functions, and Constructors →  Predefined Data Types →  Built-In ABAP Types →  Predefined Numeric Types 

Value Ranges of Packed Numbers

This example demonstrates the value ranges of packed numbers with different lengths and decimal places.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA:length   TYPE i VALUE 2,
         decimals TYPE i VALUE 2.
    cl_demo_input=>add_field( EXPORTING text  = `Length`
                             CHANGING  field = length ).
    cl_demo_input=>request(   EXPORTING text  = `Decimals`
                             CHANGING  field = decimals ).
    DATA dref TYPE REF TO data.
    FIELD-SYMBOLS <pack> TYPE p.
    TRY.
        IF decimals > 2 * length - 1.
          RAISE EXCEPTION TYPE cx_sy_create_data_error.
        ENDIF.
        CREATE DATA dref TYPE p LENGTH length DECIMALS decimals.
        ASSIGN dref->* TO <pack>.
      CATCH cx_sy_create_data_error.
        cl_demo_output=>display( 'Wrong input values ...' ).
        LEAVE PROGRAM.
    ENDTRY.

    DATA(lower)
      = cl_abap_exceptional_values=>get_min_value( <pack> ).
    IF lower IS NOT INITIAL.
      ASSIGN lower->* TO FIELD-SYMBOL(<lower>).
      cl_demo_output=>write_data( <lower> ).
    ENDIF.

    ASSERT <lower> =  CONV decfloat34(
     ( ipow( base = -10 exp = 2 * length - 1 ) + 1 ) /
       ipow( base  = 10 exp = decimals ) ).

    DATA(upper)
       = cl_abap_exceptional_values=>get_max_value( <pack> ).
    IF upper IS NOT INITIAL.
      ASSIGN upper->* TO FIELD-SYMBOL(<upper>).
      cl_demo_output=>write_data( <upper> ).
    ENDIF.

    ASSERT <upper> = CONV decfloat34(
     ( ipow( base = +10 exp = 2 * length - 1 ) - 1 ) /
       ipow( base  = 10 exp = decimals ) ).

    cl_demo_output=>display( ).

Description

The value range of a packed number with the length length and decimals decimal places is one of the following (in accordance with the formula in this table):

( -10^(2xlength-1) +1 ) / ( 10^decimals )

to

( +10^(2xlength-1) -1 ) / ( 10^decimals )

The program creates a packed number with a length and decimal places that can be entered from outside. The minimum and maximum values of the number are determined using methods from the system class CL_ABAP_EXCEPTIONAL_VALUES and compared with self-calculated values.