Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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:

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:

    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:

    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.

3 Comments
Labels in this area