Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert

Recently I stumbled over the examples in the documentation of built-in functions ROUND and RESCALE.

(Does anybody use RESCALE?)

There are tables with results of these functions for different values of the arguments, but there was no coding example how to achieve these results. I guess,  the functions were called  with different arguments one by one and the results were copied into the documentation one by one.

But, hey, we have other possibilities in ABAP now and I've tried to recreate the results with CL_DEMO_OUTPUT and REDUCE (as a recreational measure so to say).

I cannot refrain from showing you that (a bit of showing off):

TYPES:
   BEGIN OF line,
     arg       TYPE i,
     result    TYPE decfloat34,
     scale     TYPE i,
     precision TYPE i,
   END OF line,
   result TYPE STANDARD TABLE OF line WITH EMPTY KEY.

DATA(val) = CONV decfloat34( '1234.56789 ' ).
DATA(out) = cl_demo_output=>new(
   )->begin_section( 'Value'
   )->write(
     |{ val
      }, scale = { cl_abap_math=>get_scale( val )
      }, precision = { cl_abap_math=>get_number_of_digits( val ) }|
   )->begin_section( 'Round with dec'
   )->write(
    REDUCE result(
      INIT tab TYPE result
      FOR i = -5 UNTIL i > 6
      LET rddec = round( val = val dec = i
          mode  = cl_abap_math=>round_half_up ) IN
      NEXT tab = VALUE #( BASE tab
       ( arg = i
         result = rddec
         scale = cl_abap_math=>get_scale( rddec )
         precision = cl_abap_math=>get_number_of_digits( rddec )
       ) ) )
   )->next_section( 'Round with prec'
   )->write(
    REDUCE result(
      INIT tab TYPE result
      FOR i = UNTIL i > 10
      LET rdprec = round( val = val prec = i
          mode   = cl_abap_math=>round_half_up ) IN
      NEXT tab = VALUE #( BASE tab
       ( arg = i
         result = rdprec
         scale = cl_abap_math=>get_scale( rdprec )
         precision = cl_abap_math=>get_number_of_digits( rdprec )
       ) ) )
   )->next_section( 'Rescale with dec'
   )->write(
    REDUCE result(
      INIT tab TYPE result
      FOR i = -5 UNTIL i > 8
      LET rsdec = rescale( val = val dec = i
          mode  = cl_abap_math=>round_half_up ) IN
      NEXT tab  = VALUE #( BASE tab
       ( arg = i
         result = rsdec
         scale = cl_abap_math=>get_scale( rsdec )
         precision = cl_abap_math=>get_number_of_digits( rsdec )
       ) ) )
   )->next_section( 'Rescale with prec'
   )->write(
    REDUCE result(
      INIT tab TYPE result
      FOR i = UNTIL i > 12
      LET rsprec = rescale( val = val prec = i
          mode   = cl_abap_math=>round_half_up ) IN
      NEXT tab = VALUE #( BASE tab
       ( arg = i
         result = rsprec
         scale = cl_abap_math=>get_scale( rsprec )
         precision = cl_abap_math=>get_number_of_digits( rsprec )
       ) ) )
   )->display( ).

Giving

...

Isn't REDUCE just awesome? For me, one of the most powerful of all the constructor operators. Use it to get used to it. Believe me, after some training, you even don't have to look up its documentation any more.

14 Comments