Did you ever feel the need to format numbers in BRFplus? Well I did.

tl;dr

BRF+ does not offer a function for formatted number output, but this can be very easily fitted via trivial ABAP coding.

Business requirement

I recently had to implement a business check rule for characteristic values from Material classification. Numeric values are stored as floating point value (ABAP type f) in classification, even if they are defined as integer values (no decimal places). My existing check table on the other hand used NUMC as datatype, which gets turned into a BRF+ Text field (basically a String). When comparing values, BRF+ converts the float value to Text, but sadly in scientific format (e.g. 1.23E+02, instead of 123).

Implementation

OK, forget my obscure business requirement. The solution is shorter & easier to understand than the requirement. 😏

“Coding”

I hardly dare calling this assembly of definitions and built-ins (namely string templates) call “code”. Arguably, you have to start SE80 to copy the following into a class of your choice.

Note the usage of BRF+ internal types in the method parameters: This avoids type conversion overhead when going from BRF+ into ABAP method, thus providing excellent performance. This is similar to the approach for removing duplicate values from a BRF+ table.


class Y0MX_MDG_BO_BRF_RULE_UTIL definition
  public final create public .
public section.
  class-methods FORMAT_NUMBER
    importing
      !IV_NUMBER type IF_FDT_TYPES=>ELEMENT_NUMBER
      !IV_FORMAT_WIDTH type I default 0
      !IV_FORMAT_NUMBER type I default CL_ABAP_FORMAT=>N_RAW
      !IV_FORMAT_STYLE type OUTPUTSTYLE default CL_ABAP_FORMAT=>O_SIMPLE
      !IV_FORMAT_ALIGN type I default CL_ABAP_FORMAT=>A_LEFT
    exporting
      !EV_FORMATTED_NUMBER type IF_FDT_TYPES=>ELEMENT_TEXT .
ENDCLASS.
CLASS Y0MX_MDG_BO_BRF_RULE_UTIL IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method Y0MX_MDG_BO_BRF_RULE_UTIL=>FORMAT_NUMBER
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_NUMBER                      TYPE        IF_FDT_TYPES=>ELEMENT_NUMBER
* | [--->] IV_FORMAT_WIDTH                TYPE        I (default =0)
* | [--->] IV_FORMAT_NUMBER               TYPE        I (default =CL_ABAP_FORMAT=>N_RAW)
* | [--->] IV_FORMAT_STYLE                TYPE        OUTPUTSTYLE (default =CL_ABAP_FORMAT=>O_SIMPLE)
* | [--->] IV_FORMAT_ALIGN                TYPE        I (default =CL_ABAP_FORMAT=>A_LEFT)
* | [<---] EV_FORMATTED_NUMBER            TYPE        IF_FDT_TYPES=>ELEMENT_TEXT
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD format_number.
    ev_formatted_number = |{ iv_number
      WIDTH = iv_format_width
      STYLE = (iv_format_style)
      NUMBER = (iv_format_number)
      ALIGN = (iv_format_align)
    }|.
  ENDMETHOD.
ENDCLASS.

BRFplus

Use a BRF+ Procedure Call expression to invoke your string formatting. Just pass in the number and some options of your liking. Notice that for the typed input parameters like output style (IV_FORMAT_STYLE), the system automagically presents you / your business users with search helps and speaking texts:

procedure_call.PNG

In Action

Since my above stated business case (converting float value to NUMC w/o falling into scientific notation) is hard to simulate, I show you some simple Excel-like usage in the BRF+ Workbench using its fantastic simulation mode. In this example, I use data objects with domain fixed values for the procedure parameters instead of the magic numbers that the ABAP backend uses.

Some number” is the number we want to convert into formatted text output. The other values contain the format parameters:

  • Scientific output style, raw (internal) numbering format:
    simulation_scientific.PNG
    Note that the input number uses the decimal marker from my German location (a comma), while the output uses the standardized dot, used internally by the system.
  • Centered, number format from current user record (here: German), some other scientific output style:
    /wp-content/uploads/2015/03/simulation_centered_669260.png
    Since the workbench CONDENSEs the output, I have added some bars to highlight the effect.

Conclusion

BRF+ is hands down the only choice for implementing business rules in an ABAP system. It is feature rich, understandable for the business user, yet powerful, reliant and high performing. And you never “get stuck” in your implementation, because you can always extend its functionality by trivial ABAP coding. Your requirements are the limit.

To report this post you need to login first.

3 Comments

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

  1. Jocelyn Dart

    Hi Jurgen,

    It’s an interesting approach. I’m wondering why you chose this over using a formula function like TO_STRING to format the result? Just curious.

    Perhaps an example with more than just zeroes would help us understand…?

    Rgds,

    Jocelyn

    (0) 
    1. Jürgen Lukasczyk Post author

      Hi Jocelyn,

      I didn’t find an option to influence the output format in TOSTRING. Is there one? In the documentation, TOSTRING has only one input parameter.

      But you’re right, this is not really illustrative in my post. The parameters shown are also just the format options, not the numbers to be formatted. I’ll update it.

      BTW, huge fan of that workflow book! 🙂

      Best regards

      Jürgen

      (0) 
      1. Jocelyn Dart

        More the merrier Jurgen… always good to find interesting new ways… and yes I think I see your point but please a clearer illustration would be much appreciated.

        Enjoy the book! 🙂

        (0) 

Leave a Reply