Skip to Content

Dealing with Currency in SAP Script - Strange Behavior

May 23, 2017 at 04:07 AM


avatar image

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.




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!

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Sandra Rossi 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 !

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Thanks for the solution, Sandra. However, I just need one simple clarification. I was going through some help on this topic and I found this:

As per this, we should always set the checkbox. But, you have mentioned it as an obsolete flag. Can you please explain a little further?



I think you were referring to 'deactivating the flag is obsolete'.



Good finding.

In fact, it's just obvious to me that such a feature is just too much complicated and should be abandoned.

Raymond Giuseppi
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)

10 |10000 characters needed characters left characters exceeded
Matthew Billingham
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.

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Sorry, I am not so clear with what you meant. Can you please be a little elaborative? Just to mention as an extra info, we are passing REGUP-DMBTR from the script to the subroutine.

However, I am mainly bothered about how and why the decimal places got automatically shifted to have the correct value in the output.



Go to SE11. Look at the entry for DMBTR with the Currency/Quantity fields tab. You'll see it refers T001 WAERS as currency. That means that the formatting of the amount will be according to whatever value you have at that time in t001-waers. (If you have that structure in your script).

Depending on the configuration entries for the specific currency, you'll have automatically shifted decimals when you display the value. If you output a currency field using WRITE, you must use:

WRITE gv_dmbtr CURRENCY local_currency.


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.