Abap Predefined function nmax and nmin

As part of the ABAP predefined functions series, today I wanted to talk about numerical Extremum functions: NMIN, and NMAX.

These functions return the minimum/maximum values out of the input argument.

Syntax:
result = nmax|nmin( val1 = arg1 val2 = arg2....val5 = arg5 ..val9 = arg9 ).

Features:
- All the input arguments are of type numeric data object/ numeric expressions.
- A minimum of two arguments must be passed up to 9.

Example:
lv_max = nmax( val1 = 2
val2 = 6 ). "lv_max = 5
lv_min = nmin( val1 = '2'
val2 = '5' )." lv_min = 2

It's elementary to start undermining the function as an extreme of two can be easily calculated with a simple if-else:
data val1 type i value 2.
data val2 type i value 4.
if val1 <= val2.
lv_max = va1.
else.
lv_min = val2.
endif.

but what if 3, or 4 or more fields were involved:
data val1 type i value 1.
data val2 type i value 2.
data val3 type i value 3.
data val4 type i value 4.
max = val1.
if val2 > max.
max = val2.
endif.
if val3 > max.
max = val3.
endif.
if val4 > max.
max = val4.
endif.
You can notice how the ifs are increasing but it's a simple method call to NMAX or NMIN to achieve the same. Below is a small ready-to-execute report to calculate the least or highest value of input values of up to 9 variables,
REPORT sy-repid.
PARAMETERS:
p_val1 TYPE i OBLIGATORY,
p_val2 TYPE i OBLIGATORY,
p_val3 TYPE i,
p_val4 TYPE i,
p_val5 TYPE i,
p_val6 TYPE i,
p_val7 TYPE i,
p_val8 TYPE i,
p_val9 TYPE i.
PARAMETERS:
p_min TYPE abap_bool RADIOBUTTON GROUP r1 DEFAULT 'X',
p_max TYPE abap_bool RADIOBUTTON GROUP r1.
START-OF-SELECTION.
IF p_min = abap_true.
cl_demo_output=>display( data = | Least value: { nmin( val1 = p_val1
val2 = p_val2
val3 = p_val3
val4 = p_val4
val5 = p_val5
val6 = p_val6
val7 = p_val7
val8 = p_val8
val9 = p_val9 ) } |
).
ELSE.
cl_demo_output=>display( data = | Highest value: { nmax( val1 = p_val1
val2 = p_val2
val3 = p_val3
val4 = p_val4
val5 = p_val5
val6 = p_val6
val7 = p_val7
val8 = p_val8
val9 = p_val9 ) } |
).
ENDIF.
As always, I hope this information helps you with your day-to-day work.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP