###### Technical Articles

# Rounding Logic Documentation: Rounding from 0.5 in SAP ABAP

Purpose

The purpose of this updated rounding logic is to adjust a calculated numeric value, referred to as the “Number of Pieces” (lv_nop), based on a specific range of fractional parts. The goal is to round the calculated value to the nearest whole number when the fractional part falls within the range of 0.5 or greater, effectively rounding up to the nearest whole number if the fractional part is 0.5 or higher, and rounding down if it is less than 0.5.

Context

This rounding logic is applied within a SAP ABAP context, where the value of lv_nop is computed by dividing a given quantity (lv_qty) by a unit value (gwa_9013_n11-zzunit). The objective is to ensure that calculated values with fractional parts at or above 0.5 are rounded up to the next whole number, and values with fractional parts below 0.5 are rounded down, thereby achieving more accurate quantity calculations.

Logic Explanation

The initial step involves the calculation of lv_nop by performing the division of lv_qty by the unit value gwa_9013_n11-zzunit.

Following the calculation, the logic verifies whether the fractional part of lv_nop is 0.5 or greater. This check determines whether rounding is needed.

If the fractional part is 0.5 or greater, it signifies that the calculated value is very close to the next whole number. In this situation, the value of lv_nop is rounded up to the nearest whole number using the CEIL function.

However, if the fractional part is less than 0.5, the rounding logic rounds down the value of lv_nop to the nearest whole number using the TRUNC function.

Example

For instance, if lv_qty is 50 and gwa_9013_n11-zzunit is 3.5, the calculated lv_nop would be approximately 14.286. Since the fractional part (0.286) is less than 0.5, the rounding logic triggers rounding down. The value of lv_nop is rounded down to the nearest whole number, resulting in 14.

Benefits

This updated rounding logic caters to scenarios where precision is crucial, especially when fractional parts are involved. By rounding values at or above 0.5 up to the next whole number and rounding down values below 0.5, the logic ensures accurate quantity calculations.

Recommendations

Thorough testing and review of the logic across different cases are recommended to confirm alignment with the desired behavior and business requirements. Additionally, consider adjusting the threshold value of 0.5 if different levels of precision are needed to meet specific business needs.

Certainly! Let’s use an example with sample input values to demonstrate the code and its output:

Suppose we have the following values:

lv_qty (Input Quantity) = 50. gwa_9013_n11-zzunit (Unit Value) = 3.5.

Here’s the code with these values:

```
REPORT z_rounding_example.
DATA: lv_qty TYPE i,
gwa_9013_n11 TYPE z_gwa_9013_n11, " Replace with your structure type
lv_nop TYPE f,
lv_fractional TYPE f.
" Sample input values
lv_qty = 50.
gwa_9013_n11-zzunit = 3.5.
IF gwa_9013_n11 IS NOT INITIAL.
lv_nop = lv_qty / gwa_9013_n11-zzunit.
lv_fractional = lv_nop - trunc( lv_nop ).
IF lv_fractional >= 0.5.
lv_nop = ceil( lv_nop ).
ELSE.
lv_nop = trunc( lv_nop ).
ENDIF.
ENDIF.
WRITE: 'Input Quantity (lv_qty):', lv_qty,
/ 'Unit Value (gwa_9013_n11-zzunit):', gwa_9013_n11-zzunit,
/ 'Calculated Number of Pieces (lv_nop):', lv_nop.
```

When you run this updated code with the provided input values, it will display the following output:

Input Quantity (lv_qty): 50 Unit Value (gwa_9013_n11-zzunit): 3.5 Calculated Number of Pieces (lv_nop): 14

In this output, you can see that the calculated number of pieces (lv_nop) is 14, which is the result of rounding down the fractional part (0.286) to the nearest whole number because it is less than 0.5.

#### Author

**Prathamesh Rawool**

Of course you could omit the IF... ENDIF clause, by simply using

lv_nop = trunc( lv_nop + 0.5 ).