SAP BOX

'26/11/29 UPDATE

ABAP / FIELD NOTES

Why does WRITE TO turn the same amount into 1.234.567,89 for one user and 1,234,567.89 for another?

WRITE … TO takes the decimal and thousands separators of a number from the user master defaults (the format for decimals in SU01 or SU3) or from a country format in T005X set with SET COUNTRY. So the same code gives 1.234.567,89 or 1,234,567.89 depending on who runs it. A string template defaults to NUMBER = RAW and always gives 1234567.89. Based on the ABAP 7.58 documentation, this note compares three lines of code, explains SET COUNTRY versus COUNTRY =, and shows why files and interfaces should use RAW.

The same Z program turns the same amount field into a string with WRITE … TO, yet the file one user's run produces shows 1.234.567,89 and the file another user's run produces shows 1,234,567.89. Not a single line of code differs. After reading this, you will be able to tell where the difference comes from (the user defaults and SET COUNTRY), why the string template |{ amt }| always gives 1234567.89, and which format to choose for files and interfaces. The basis is the ABAP Keyword Documentation, release 7.58. The users and values in the examples are fictional; the results apply the documented rules by hand and were not run on an SAP system.

WRITE … TO takes its separators from the user defaults

The predefined output format of WRITE … TO depends on the data type and on user-specific default settings. A packed number (p) keeps its defined decimal places and gets a decimal separator, and if the target field is long enough it also gets thousands separators. Which characters are used is decided by the formatting setting of the language environment.

At the start of an internal session, the formatting setting is taken from the Defaults in the user master record of the current user. The number format has three values.

  • Blank: comma as the decimal separator, period for thousands. 1.234.567,89
  • X: period as the decimal separator, comma for thousands. 1,234,567.89
  • Y: comma as the decimal separator, blank for thousands. 1 234 567,89

The user default is stored in column DCPFM of table USR01 and uses the same domain (XUDCPFM) as column XDEZP of table T005X, which holds the country-specific formats. Administrators change it in SU01, and users change their own in SU3 (System > User profile > Own data). SAP's documentation calls this default "the format for decimals". The same default also applies to the numbers shown in dynpro screen fields.

In the screen example below, USER_A has the blank value and gets 1.234.567,89, while USER_B has X and gets 1,234,567.89. There are three values to choose from, and which one applies is stored separately for each user.

Example SAP GUI screen. Transaction SU01 with the Defaults tab open. The group User USER_A shows Logon Language EN and Decimal Notation with the blank value 1.234.567,89; the group User USER_B shows Logon Language EN and Decimal Notation X, 1,234,567.89. A line below lists the three values: blank 1.234.567,89, X 1,234,567.89 and Y 1 234 567,89. Marker 1 is on USER_A's field, 2 on USER_B's field and 3 on the value line.
Example of the SU01 Defaults tab, not a capture. Two fictional users are drawn side by side on one screen. The tab row, the menu, the field label Decimal Notation and the look of the value list were not seen on a real system, and only a few tabs needed for this note are drawn. SAP's documentation calls this default "the format for decimals", and the separators of the three values come from the number format table in the ABAP documentation. Markers 1 and 2 match the body sentence on the format USER_A and USER_B get, and marker 3 the sentence on the three values to choose from.

One value, 1234567.89, different results by code

Take one fictional amount variable and turn it into a string in three ways.

DATA amt TYPE p LENGTH 8 DECIMALS 2 VALUE '1234567.89'.
DATA txt TYPE c LENGTH 20.
DATA s1  TYPE string.
DATA s2  TYPE string.

WRITE amt TO txt.                 " (1)
s1 = |{ amt }|.                   " (2)
s2 = |{ amt NUMBER = USER }|.     " (3)

Applying the documented rules gives these results.

CodeUSER_A (blank)USER_B (X)
(1) WRITE amt TO txt.1.234.567,891,234,567.89
(2) |{ amt }|1234567.891234567.89
(3) |{ amt NUMBER = USER }|1.234.567,891,234,567.89

(1) follows the user default described above. The result is right-aligned in txt, and the rightmost place is kept for the sign. It is blank for a positive value and holds - for a negative one, so -1234567.89 becomes 1.234.567,89- for USER_A.

(2) is the same whoever runs it. A number in a string template, with no option given, always uses a period as the decimal separator and gets no thousands separators. That is the default value RAW of the option NUMBER, which controls the shape of the number. A negative value gets its minus sign on the left with no blank, -1234567.89, and the length is just what the result needs.

With NUMBER = USER, as in (3), the separators come from the user master record, so the characters split the same way as in (1). Alignment and leading blanks still follow the string template rules. NUMBER = ENVIRONMENT follows the current formatting setting of the language environment; in an internal session where SET COUNTRY has not been run, it gives the same result as USER. The figure below adds this fourth case.

SAP NOTES

One value, 1234567.89, four ways to format it

amt TYPE p LENGTH 8 DECIMALS 2 · user A blank, user B X

  • WRITE amt TO txt.

    A 1.234.567,89 · B 1,234,567.89

    • Split by the user default
    • Right-aligned in txt, last place kept for the sign
  • |{ amt }|

    A 1234567.89 · B 1234567.89

    • Default NUMBER = RAW
    • Period as decimal separator, no thousands separators
  • |{ amt NUMBER = USER }|

    A 1.234.567,89 · B 1,234,567.89

    • Separators from the user master record
    • Left-aligned, no leading or trailing blanks
  • |{ amt NUMBER = ENVIRONMENT }|

    A 1.234.567,89 · B 1,234,567.89

    • Same as USER until SET COUNTRY runs
    • After SET COUNTRY 'xx': that country's T005X format

Users, values and outputs are fictional. The ABAP 7.58 rules were applied by hand; nothing was run on an SAP system.

The same p field turned into a string four ways. WRITE … TO, NUMBER = USER and (before SET COUNTRY) NUMBER = ENVIRONMENT differ by user; a string template with no option gives 1234567.89 whoever runs it.

SET COUNTRY changes the format of every later statement in the internal session

SET COUNTRY is the statement that switches from the user defaults to a country-specific format. Pass a country key from column LAND of T005X, and the number, date, time and time stamp formats follow that row's XDEZP, DATFM and TIMEFM. It applies to every following statement in the current internal session.

  • An initial value (a blank in the first position) returns to the defaults in the user master record.
  • A key that is not in T005X sets sy-subrc to 4, with a period as the decimal separator, a comma for thousands and dates as mm/dd/yyyy.
  • There is no GET COUNTRY statement that reads the current setting.

So using SET COUNTRY to fix one amount also changes the other WRITE … TO statements, list output and string templates with ENVIRONMENT in the same internal session, and the date format changes along with them.

To format a single value in a country's format, the string template option COUNTRY = is narrower. |{ amt COUNTRY = lv_land }| uses the T005X format for that one expression only and leaves later statements alone. If lv_land (type LAND1) is initial, the user master defaults are used; a value that is not in T005X raises the exception CX_SY_STRG_FORMAT. This option also inserts the thousands separators that string templates leave out by default.

Which separators a country key uses is decided by T005X in that system. The ABAP documentation also gives 1,000,000 as the result of COUNTRY = 'DE ' and adds that it depends on the content of T005X, so do not guess from the country name; check the XDEZP value.

Use RAW for files and interfaces

The trouble grows with strings that a program reads, not a person. When an amount made with WRITE … TO goes into a file, the file keeps the format of whoever ran the program.

It is even clearer when the receiver is ABAP. When a character-like field is assigned to a packed or integer field such as p or i, the accepted notation is an unbroken run of digits with at most one period as the decimal separator (and a sign in front or behind). By this rule neither 1.234.567,89 nor 1,234,567.89 is read as a number, and the exception CX_SY_CONVERSION_NO_NUMBER is raised. 1234567.89 fits the rule. Inside an ABAP program the decimal separator is always a period. The characters that differ by user appear in formatted output.

Adding NO-GROUPING to WRITE … TO removes only the thousands separators; the decimal separator still follows the formatting setting, so USER_A gets 1234567,89. So for strings that a program reads again, such as files, interfaces and key values, use |{ amt }|, and write NUMBER = RAW if you want the intent to be visible. String templates were introduced in ABAP release 7.0, EhP2 (7.02), so systems older than that cannot use this approach. Before you swap one for the other, check two things.

  • If the original line had CURRENCY waers, carry the option over: |{ amt CURRENCY = waers }|. Without it, the decimal places follow the variable's definition instead of the currency (TCURX); what that does to won, a currency with no decimal places, is in the note on why a won amount is read as 10,000.00.
  • For a fixed-length record, or a partner that expects right alignment and a trailing sign, keep the separators RAW and set position and sign with WIDTH, ALIGN = RIGHT and SIGN = RIGHT. ALIGN only has an effect when WIDTH makes the length longer, and unlike WRITE … TO, SIGN = RIGHT leaves no blank after a positive value, so check the column positions against the partner's specification.

If the partner system needs a specific country format, COUNTRY = on that one value is better than SET COUNTRY, because it does not affect other output. Which user's defaults apply in a background job was not checked for this note, but with RAW the file looks the same whatever the answer is.

SAP NOTES

Amounts that leave in a file: use RAW

Same program, only the user who runs it differs

  • Line written with WRITE amt TO txt.

    1.234.567,89 / 1,234,567.89

    • The runner's format ends up in the file
    • Right-aligned, so leading blanks come along
  • Receiving ABAP: lv_amt = field.

    CX_SY_CONVERSION_NO_NUMBER

    • Valid: unbroken digits, at most one period
    • Neither string with separators fits the rule
  • Line written with |{ amt }|

    1234567.89

    • The same string whoever runs it
    • Also fits the ABAP number notation rule

The file, fields and values are fictional. The conversion results apply the ABAP 7.58 number notation rule by hand; nothing was run.

A line made with WRITE … TO carries the runner's format, and neither format is a notation ABAP accepts as a number. A string template with no option (RAW) gives 1234567.89 regardless of who runs it.

Cases this note does not cover

  • decfloat16 and decfloat34: the rules differ (for example, WRITE … TO does not show trailing zeros in the decimal places), and string templates have a separate STYLE option for them.
  • Currency amount fields: how amounts are stored for each currency is covered in the note linked above.
  • Dates and times: the same formatting setting applies, but this note covers numbers only.

The full list of options is in the ABAP Keyword Documentation under string template format options (7.58).

There is one place to check. On a system with string templates (7.0 EhP2 and later), find WRITE … TO in the lines that build amounts for files or other systems and change it to a string template, checking the two points above (CURRENCY and fixed length) as you go. That line then gives the same string whoever runs it, and the per-user format stays on the screens people look at.

THE END
RSS feedBack to the library
COMMENTS BOX

Add your perspective.

Share a question, another approach, or something you have tried.

Newest first

Checking sign-in…

Loading comments…