SAP DEC FLOATING POINT FUNCTIONS



Get Example source ABAP code based on a different SAP table
  


• rescale ABAP_FUNCTION
• round ABAP_FUNCTION
• val round function
• val rescale function
• mode round function
• mode rescale function
• dec round function
• dec rescale function
• prec round function
• prec rescale function

ABAP_NUMFUNC - round, rescale

ABAP_SYNTAX
... round$|rescale( val = arg ... ) ...

What does it do?
The built-in rounding functions expect a decimal floating point number as a main argument val and additional arguments that describe how this floating point number is handled. The type of the return value of a rounding function is always decfloat34 . Within an arithmetic expression, the argument for the decimal floating point number can either be an arithmetic expression or a function. The other arguments must always be specified as numeric data objects.
ITOC



Latest notes:

The class CL_ABAP_MATH includes the method NORMALIZE for normalizing a decimal floating point object. The mantissa does not have any closing zeros in a normalized floating point number.
NON_V5_HINTS
ABAP_HINT_END

Rounding Function
The rounding function round can be implemented in operand positions using the following syntax:

ABAP_SYNTAX
... round( val = arg ${dec = n$}$|${prec = n$} $[mode = m$] ) ...

What does it do?
This function rounds a decimal floating point number specified as an argument for the parameter val. A data object specified for arg is converted to the data type decfloat34 before the function is executed, if necessary.
Either the parameter dec or the parameter prec must be given a value, and rounding must be to either a particular number of decimal places or to a precision:
If the parameter dec is given a value, the input value is rounded to the number of decimal places specified in n and returned. n expects data objects of the type i whose value must not be less than -6144. If a negative value is specified, the corresponding pre-decimal place is rounded.
If the parameter prec is given a value, the value entered is rounded to the precision specified in n and returned. n expects data objects of the type i whose value must be greater than 0.
A rounding can reduce scaling and precision but cannot increase them. If dec is specified, the mantissa of the return value does not contain any zeros after the place where the rounding applies. If prec is specified, the input value is returned unchanged if the specified precision is greater than or equal to the input value.
The optional parameter mode determines the rounding type. For m it is only possible to specify values that exist as ROUND_... constants in class CL_ABAP_MATH. The following table shows the possible rounding rules. If mode is not given a value, commercial rounding is used. ConstantRounding rule ROUND_HALF_UPThe value is rounded down to the nearest rounded value. If the value falls exactly halfway between two rounded values, it is rounded away from zero (commercial rounding). ROUND_HALF_DOWNThe value is rounded down to the nearest rounded value. If the value falls exactly halfway between two round values, it is rounded towards zero. ROUND_HALF_EVENThe value is rounded down to the nearest rounded value. If the value falls exactly halfway between two rounded values, it is rounded to the value whose last place is an even number. ROUND_UPThe value is always rounded away from zero or towards the greater absolute value. ROUND_DOWNThe value is always rounded from zero or towards the lesser absolute value. ROUND_CEILINGThe value is always rounded in a positive direction or towards the greater value. ROUND_FLOORThe value is always rounded in a positive direction or towards the greater value.

ABAP_EXAMPLE_VX5
The following tables show the results of commercial rounding of the decimal floating point number 1234.56789 (scaling 5, precision 9) with different values for dec and prec. The shown results shown are created by executing the class CL_DEMO_ROUND_AND_RESCALE. decResultScalingPrecision -50E+5-51 -40E+4-41 -31E+3-31 -21.2E+3-22 -11.23E+3-13 0123504 11234.615 21234.5726 31234.56837 41234.567948 51234.5678959 61234.5678959 precResultScalingPrecision 11E+3-31 21.2E+3-22 31.23E+3-13 4123504 51234.615 61234.5726 71234.56837 81234.567948 91234.5678959 101234.5678959
ABAP_EXAMPLE_END

ABAP_EXAMPLE_ABEXA
Rounding Function round
ABAP_EXAMPLE_END

Rescaling Function
The rescaling function rescale can be implemented in operand positions using the following syntax:

ABAP_SYNTAX
... rescale( val = arg ${dec = n$}$|${prec = n$} $[mode = m$] ) ...

What does it do?
This function changes the scaling of a decimal floating point number specified as an argument for the parameter val . A data object specified for arg is converted to the data type decfloat34 before the function is executed, if necessary.
Either the parameter dec or the parameter prec must be given a value, where either the scaling or the precision is set:
If the parameter dec is given a value, the input value is returned using the scaling specified in n . n expects data objects of the type i , whose value must not be less than -6144. If the scaling would produce more than 34 places in the mantissa of the return value, a catchable exception is raised.
If the parameter prec is given a value, the value entered is returned with the precision specified in n and appropriate scaling. n expects data objects of the type i, whose value must be greater than 0 and less than 34.
A rescaling can both reduce and increase scaling and precision. An increase adds zeros on the right.
The input value is rounded if required. The optional parameter mod can be used to specify the rounding rule, as described under the function round. The default is commercial rounding.

ABAP_EXAMPLES
The following tables shows the results for rescaling of the decimal floating point number 1234.56789 (scaling 5, precision 9) with various values for dec and prec, if commercial rounding is used. The results shown are generated by executing the class CL_DEMO_ROUND_AND_RESCALE. decResultScalingPrecision -50E+5-51 -40E+4-41 -31E+3-31 -21.2E+3-22 -11.23E+3-13 0 123504 11234.615 21234.5726 31234.56837 41234.567948 51234.5678959 61234.567890610 71234.5678900711 81234.56789000812 precResultScalingPrecision 1 1E+3-31 2 1.2E+3-22 3 1.23E+3-13 4 123504 5 1234.615 6 1234.5726 7 1234.56837 8 1234.567948 9 1234.5678959 10 1234.567890610 11 1234.5678900711 12 1234.56789000812

ABAP_EXAMPLE_END