Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does a string template return different formatting for types with 2 and 4 decimal places?

I have the following piece of code. I want to format number with string templates. One variable has 2 decimal places, the other 4 decimal places but they represent the same number 50000 (fifty thousand).

The first number is correctly formatted (German representation) 50.000,00, the other one however is formatted as 5 million 5.000.000,00!

DATA: lv_p2 TYPE p LENGTH 9 DECIMALS 2,
      lv_p4 TYPE p LENGTH 14 DECIMALS 4.

START-OF-SELECTION.
  lv_p2 = '50000'.
  lv_p4 = lv_p2.

SET COUNTRY 'DE'.
"This is correctly formatted as 50.000,00
WRITE |{ lv_p2 NUMBER = ENVIRONMENT CURRENCY = 'EUR' }|.

"This is on the other hand interpreted as five million! 5.000.000,00
WRITE |{ lv_p4 NUMBER = ENVIRONMENT CURRENCY = 'EUR' }|.

Is this documented somewhere? What am I doing wrong here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EDIT:

It looks like the problem is with the addition CURRENCY. If I don’t use it, then the number is correctly formatted.

WRITE |{ lv_p4 NUMBER = ENVIRONMENT }|.

or WRITE |{ lv_p4 NUMBER = ENVIRONMENT DECIMALS = 2 }|.

Anyway looks like some kind of a bug.

>Solution :

I believe this behaviour is documented.

https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abapwrite_to_options.htm#!ABAP_ADDITION_6@6@

When CURRENCY is added:

"For data objects of type p, the decimal places determined by the
definition of the data type are ignored completely. Independently of
the actual value and without rounding, decimal separators and
thousands separators are inserted between the digits in the places
determined by cur."

Shortly: if CURRENCY is added (by WRITE), the number of decimal places is determined by the currency (in this case EUR has 2 decimal places), so the value 50.000,0000 will be 5.000.000,00. Same length (9 digits) only the number of decimals will be different.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading