Aug 31, 2010

Decimal Point Notation (and its Effects in Calculations)

Things can get really weird in ABAP. When you use the SAP system you can set how your decimal point notation. What is this? It is how you want your decimal to appear on screen. For example, here in the Philippines we use the comma (,) to denote a thousand and period (.) for decimals. Ex: 1,234.56. In some countries, the use a different notation – the comma (,) and period (.) are interchanged thus turning 1,234.56 to 1.234,56. It looks simple but it can be a pain in the a**!

I am using BAPI_INSPOPER_GETDETAIL to get the Inspection Lot and Operator data. This function module returns a table of the Characteristics in CHAR_RESULTS which has an associated type of BAPI2045D2. I need to use the data from CHAR_RESULTS-MEAN_VALUE for calculating the price of something. The problem is that MEAN_VALUE is type CHAR and my system is hard-locked to use the 1.234,56 decimal point notation. Example MEAN_VALUE = 1.000,25 then let’s say my calculation is MEAN_VALUE + 1. This will lead to a short dump because 1.000,25 is interpreted as a text not numbers so automatic conversion (which usually happens when the data type is a number) from 1.000,25 to 1,000.25 doesn’t happen. Here’s a way I found to get around this error.

First check the user’s settings for the decimal pt. notation.

SELECT SINGLE dcpfm FROM usr01 INTO l_dcpfm WHERE bname EQ sy-uname.

Then change all occurrences of decimal (.) to space in MEAN_VALUE. This will ensure that thousands stay intact. So 1.000,25 becomes 1 000,25.

REPLACE ALL OCCURRENCES OF '.' IN MEAN_VALUE WITH space.

Then change all occurrences of comma (,) with period (.). So 1 000,25 becomes 1 000.25.

REPLACE ALL OCCURRENCES OF ',' IN MEAN_VALUE WITH '.'.

Then condense MEAN_VALUE so the space is removed thus getting the value of 1000.25 which is the SAP format for numbers. Take note that MEAN_VALUE is of type CHAR (character).

CONDENSE MEAN_VALUE.

Finally move MEAN_VALUE to L_VALUE which is of type numeric (in my case TYPE p DECIMALS 2).

MOVE MEAN_VALUE TO L_VALUE.

And the short dump should be averted.

Is there anything wrong with this post? Or with the code? Or with the explanation? Let me know in the comments section.

0 comments:

Post a Comment