Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

REDUCE a decilmal value

marcelom_bovo
Participant
0 Likes
14,464

Hi People.

I'm trying to use REDUCE to sum some decimal fields of a internal table but the result is being automatically rounded up.


SELECT matnr, werks, charg, clabs, cinsm, cspem

       FROM mchb

       INTO TABLE @DATA(lt_mchb)

      WHERE matnr = variable0

        AND werks = variable

        AND charg = variabl2


DATA(lv_saldo) = REDUCE LABST( INIT x = 0 FOR wa IN lt_mchb NEXT x = x + wa-clabs + wa-cinsm + wa-cspem  ).

The result should be 11.657 but LV_SALDO is receiving 12.000

What am I doing wrong?

thanks

1 ACCEPTED SOLUTION
Read only

Former Member
11,277

Hi,

Try this: INIT x = '0.000'

The declarations after INIT will create the variable as the value passed.

DATA(lv_saldo) = REDUCE labst( INIT x = '0.000' FOR wa IN lt_mchb NEXT x = x + wa-clabs + wa-cinsm + wa-cspem  ).

Douglas

Hi People.

I'm trying to use REDUCE to sum some decimal fields of a internal table but the result is being automatically rounded up.


SELECT matnr, werks, charg, clabs, cinsm, cspem

       FROM mchb

       INTO TABLE @DATA(lt_mchb)

      WHERE matnr = variable0

        AND werks = variable

        AND charg = variabl2


DATA(lv_saldo) = REDUCE LABST( INIT x = 0 FOR wa IN lt_mchb NEXT x = x + wa-clabs + wa-cinsm + wa-cspem  ).

The result should be 11.657 but LV_SALDO is receiving 12.000

What am I doing wrong?

thanks

13 REPLIES 13
Read only

Former Member
0 Likes
11,277

You are working on N/W release 7.4 or greater to which I do not have access too but if your definition of  lv_saldo is an integer, the results will always be rounded.

If you want to avoid this, then you should change the definition to either F or P.

Thanks,

Vikram.M

Read only

0 Likes
11,277

Hi, the variable is being created with type LABST which has 3 decimals

tks

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
11,277

Hi,

Check your data type LABST. It must have decimals. Replace it with such a type and it should work.

Horst

Read only

0 Likes
11,277

LABST already has three decimals

Read only

Former Member
11,278

Hi,

Try this: INIT x = '0.000'

The declarations after INIT will create the variable as the value passed.

DATA(lv_saldo) = REDUCE labst( INIT x = '0.000' FOR wa IN lt_mchb NEXT x = x + wa-clabs + wa-cinsm + wa-cspem  ).

Douglas

Read only

0 Likes
11,277

Thanks Douglas, it worked

Actually I had to put an extra zero '0.0000' because with just three zeroes after the decimal point the result was getting wrong "10,710" instead of "10,712"...weird

The values that are being summed are 2.312 + 4 + 4.4

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
11,277

You are working with a type c field now. Use the init with type addition to get a numeric type .

Read only

11,277

Hi Horst.

Now its better

DATA(lv_saldo) = REDUCE LABST( INIT x TYPE LABST FOR wa IN lt_mchb NEXT x = x + wa-clabs + wa-cinsm + wa-cspem  ).

thanks

Read only

11,277

Thanks marcelom.bovo for the answer, it helped me today while I was struggling with the rounding up an amount field.

Read only

0 Likes
6,845

Great, I also faced similar issue. Adding extra 0 worked. So '0.0000' is perfect. Thanks

Read only

0 Likes
5,903

'0.000' or '0.0000' are incorrect, they both may raise an overflow short dump. Use a numeric type with enough digits, as explained in other replies and by the original poster himself.

Read only

0 Likes
5,945

Thanks @marcelom_bovo for the answer.

it working fine.

Read only

Sandra_Rossi
Active Contributor
0 Likes
5,903

The answer by Douglas is currently marked as being the right solution, but it's buggy (an overflow short dump or decimal truncation may happen) so it's misleading people in 2025. As you noticed that it was incorrect, could you ask the moderator to unflag the solution? I don't know if the moderator can do that but it's worth trying. Thank you. Sandra