Skip to Content

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

/wp-content/uploads/2016/08/reduce_1023009.jpg

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.

To report this post you need to login first.

10 Comments

You must be Logged on to comment or reply to a post.

  1. Suhas Saha

    Isn’t REDUCE just awesome? For me, one of the most powerful of all the constructor operators. Use it to get used to it.

    I use the REDUCE (aka table-reduction) operator often in productive code. Imo, it makes the code so much cleaner 😘

    (0) 
  2. Jelena Perfiljeva

    Does anybody use RESCALE?

    Arrgh, stop rubbing it in, Horst! Our system is too old even for the “Next generation ABAP development”. 🙁

    I do appreciate your samples though, thank you!

    (0) 
      1. Sandra Rossi

        Thx. But I didn’t explain well my question. I couldn’t test it until today, but the first REDUCE is equivalent to this VALUE expression:

            VALUE result(
              FOR i = -5 UNTIL i > 6
              LET rddec = round( val = val dec = i
                  mode  = cl_abap_math=>round_half_up ) IN
              ( VALUE #(
                 arg = i
                 result = rddec
                 scale = cl_abap_math=>get_scale( rddec )
                 precision = cl_abap_math=>get_number_of_digits( rddec )
               ) ) )
        

        So, is there a reason to use REDUCE instead of VALUE? 😉

        (0) 
        1. Horst Keller Post author

          In fact no, you can replace all the REDUCEs with your VALUEs in this case 😡 .

          Let’s say it is a question of taste. When I have such a task, I start with REDUCE, because I LOVE it 😛

          PS: Sandra, your’e simply the best …

          (0) 

Leave a Reply