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: 

REDUCE a decilmal value

marcelom_bovo
Participant
0 Kudos
8,160

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

Former Member
4,973

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

10 REPLIES 10

Former Member
0 Kudos
4,973

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

0 Kudos
4,973

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

tks

retired_member
Product and Topic Expert
Product and Topic Expert
0 Kudos
4,973

Hi,

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

Horst

0 Kudos
4,973

LABST already has three decimals

Former Member
4,974

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

0 Kudos
4,973

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

retired_member
Product and Topic Expert
Product and Topic Expert
4,973

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

4,973

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

4,973

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

0 Kudos
541

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