Skip to Content
avatar image
-1
Former Member

Dealing with Currency in SAP Script - Strange Behavior

Hi Experts!

Firstly, I would like to inform that, we are aware that SET COUNTRY resolves our issue but I seriously would like to get some clear explanation for the strange behavior of usage of currency in SAP Script.

We have a subroutine being called in SAP Script and here is the code implemented within the subroutine.

FORM read_currencies TABLES in_tab TYPE gt_itcsy
out_tab TYPE gt_itcsy.

DATA: lv_amount TYPE dmbtr.

LOOP AT in_tab INTO wa_tab.

IF wa_tab-name EQ 'AMOUNT'.

REPLACE ALL OCCURENCES OF ',' IN wa_tab-value WITH space.

CONDENSE wa_tab-value.

lv_dmbtr = wa_tab-value.

ENDIF.

ENDLOOP.

ENDFORM.

The country settings have the decimal format as 1.234.567,89.

Now, let me take an example and explain what's strange. We have the amount as 24.125,78. Now when this amount goes through the code in the subroutine, wa_tab-value is initially 24.125,78. And now after REPLACE and CONDENSE, the value is 24.12578. Till here everything went as expected. And now, when we come to the assigning line, it behaves weirdly. When we move 24.12578 is moved to lv_dmbtr, we expect it to be 24.13 (if rounded off). But it is taking the value correctly as 24125.78. And now, please check the below code:

DATA: gv_dmbtr TYPE dmbtr,
gv_char TYPE char255.

gv_char = '24.12578'.
MOVE gv_char TO gv_dmbtr.

WRITE gv_dmbtr.

Surprisingly, this gives the output as 24.13. I am now confused because the code within the subroutine behaves differently. But the same code as a separate program behaves in a different way.

And now, if the value is too large (say, 123.234.345,56, it results in conversion runtime error as the value after REPLACE will have 2 dots which can't be recognized). So, now my feeling is we should have had the issue with small amounts too as explained above, but we did not.

Can someone please provide proper reasoning for this behavior?

Your help is highly appreciated.

Thanks in advance!

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    May 23, 2017 at 07:54 AM

    I agree with Raymond, about the WRITE.

    For your first question about why dmbtr = '24.12578' (with a dot, i.e. 5 digits after the decimal point) leads to the situation of dmbtr = 24125.78 (2 digits after the decimal point), that's probably due to the obsolete "fixed-point arithmetic" flag, which is NOT activated in the program attributes (at the same level of the "Unicode check" flag, program title, package and so on). When it's not activated, SAP does as if the data objects have no digits after the decimal point !

    Add comment
    10|10000 characters needed characters exceeded

  • May 23, 2017 at 06:00 AM

    Well DMBTR is a currency field, actually some DEC 13,2 WERT7, First before the statement WRITE try no to think in terms of currency but in terms of conversion rules between character and numeric types (Conversion Rules for Elementary Data Objects)

    Add comment
    10|10000 characters needed characters exceeded

  • May 23, 2017 at 05:36 AM

    The amount is probably linked to a currency field (check the structure in SE11). When this is the case, the output you get depends on how the currency is configured.

    Add comment
    10|10000 characters needed characters exceeded

    • Which is why for currency fields and quants a 'WRITE x TO s CURRENCY zzz' or 'WRITE x TO s UNIT zzz' is generally the better way to go if you are not outputting the field directly, otherwise as @Matt says - take notice of any linked currency or unit field.