'Why does ABAP round a number down to the nearest integer when using the REDUCE operator?

In the code below, lv_sum_openamount should be 3.45 but the program rounds the number as 3.

I want lv_sum_openamount as 3.

How can I do that ?

DATA(lv_sum_openamount) = REDUCE dmbtr_cs( INIT sum = 0 FOR wa_amnt IN <fs_comp> NEXT sum += wa_amnt-open_amount.

LOOP AT <fs_comp> ASSIGNING <fs_comp_alv>.
  TRY.
    <fs_comp_alv>-pull_amount = ( <fs_pack>-reamount / lv_sum_openamount ) * <fs_comp_alv>-open_amount.
  CATCH cx_sy_zerodivide.
    <fs_comp_alv>-pull_amount = 0.
  ENDTRY.
ENDLOOP.


Solution 1:[1]

The culprit is the part INIT sum = 0.

0 is an integer, so the type for sum gets automatically derived as an integer. That means that the REDUCE-loop then uses integer arithmetic, so its output is rounded down.

Try INIT sum = CONV dmbtr_cs( 0 ) instead. This will convert the literal of 0 to the type you need and in turn force sum to also get that type.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Philipp