ABAP Round functionality to round values down to nearest value based on your decimal place requirement
The ABAP ROUND statement only rounds to nearest value whether it be up or down.
data: ld_round type p DECIMALS 2 value '22.23', ld_result type p DECIMALS 2. *Rounding using the ROUND ABAP statement ld_result = round( val = ld_round dec = 2 ). "rounds ld_round to 2 decimal place write: ld_result. "result = 22.26 as it is already at 2 decimal places ld_result = round( val = ld_round dec = 1 ). "rounds ld_round to 1 decimal place write: ld_result. "result = 22.30 ld_round = '22.23'. ld_result = round( val = ld_round dec = 1 ). "rounds ld_round to 1 decimal place write: ld_result. "result = 22.20
But sometimes you may want to always round your values down. So even if the start value is 22.29 you may still want
the result to be 22.2. Therefore the following ABAP code demonstrates how to always round a number DOWN, the
exmaple shows hopw to do it from 2 decimal places to 1 dp but its the same for different variations.
Also see here for how to Round values up
*Rounds a value DOWN to 1 decimal using function module 'ROUND' DATA: dec2 TYPE p DECIMALS 2 VALUE '22.23', dec1 TYPE p DECIMALS 1, ld_str(50) TYPE c. CALL FUNCTION 'ROUND' EXPORTING decimals = 1 input = dec2 sign = '-' "Negative Rounding Concept IMPORTING output = dec1 "result will be 22.2 EXCEPTIONS input_invalid = 1 overflow = 2 type_invalid = 3 OTHERS = 4.
*Rounds a value DOWN to 1 decimal using char manipulation DATA: dec2 TYPE p DECIMALS 2 VALUE '22.23', dec1 TYPE p DECIMALS 1, ld_str(50) TYPE c. ld_str = dec2. SHIFT ld_str RIGHT DELETING TRAILING ' '. SHIFT ld_str RIGHT BY 1 PLACES. SHIFT ld_str LEFT DELETING LEADING ' '. dec1 = ld_str. "result will be 22.2
Related Articles
Retrun the fraction / whole value of a decimal number using MOD, DIV, FRAC or SHIFT
Remove spaces from character field (CONDENSE)
Move minus sign(-) from end to start of field
Round ABAP values up to the nearest value
Inserting TAB's into text strings
Change characters within a string ( TRANSLATE )