Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Character String and Byte String Processing →  Statements for Character String and Byte String Processing →  WRITE - TO 

WRITE - Predefined Formats

The predefined output format of the statements WRITE ... TO and WRITE depends on the data type and user-specific settings. It is partly dependent on the length available. In the case of WRITE ... TO, this is the length of the target variable; in the case of WRITE, this is the output length. If the length is not sufficient, the variables are truncated as specified in the predefined cutoff behavior.

Other versions: 7.31 | 7.40 | 7.54

Alignment

The following table shows the predefined alignment for the built-in ABAP types.

Data Type Alignment
Numeric data types Right-aligned
Character-like data types Left-aligned
Byte-like data types Left-aligned
Date types/time types Left-aligned


Example

The text field text is padded with blanks on the left by WRITE.

DATA text TYPE c LENGTH 40. 
DATA num TYPE i VALUE -1234. 
WRITE num TO text. 
cl_demo_output=>display( text ).

General Format

Numeric Data Types

  • In the numeric data types (b, s), i, int8, and p, the last place on the right is reserved for the sign (this also applies to b, even though these numbers are always positive). Negative values are given a minus "-" here in the result and positive values a blank. The thousand separators defined in the user master record are inserted if there is enough length available. If data type p is used, decimal places are respected (regardless of the program property fixed point arithmetic). A decimal separator is inserted where necessary (see below).

  • If enough length is available, decimal floating point numbers with the data types decfloat16 and decfloat34 are displayed in mathematical notation and with the thousand separators defined in the user master data. If there is not enough length for the fractional portion of the number, it is rounded commercially to the number of positions available. If there is not enough length for the integral part of the number, the value is formatted in scientific notation without thousand separators. Negative numbers are prefixed with a minus sign in both cases. An exponent is always displayed with a sign, but with no leading zeros. Trailing zeros in the fractional portion are not shown.

  • A binary floating point number with the type f is formatted in scientific notation, prefixed with a sign (empty if the number is positive), a place before the decimal separator, a maximum of 16 decimal places, the character "E", and a three-figure exponent, including sign.

Information about the number format with respect to decimal and thousands separators can be found below.


Notes

  • If there is not enough space to display a number using these rules, an exception is raised for decimal floating point numbers only. Other numeric types are truncated and flagged with "*".

  • It should be noted that, in the case of the data types b and s, the predefined output lengths for lists ignore the place that is reserved for the sign. This can produce unexpected results.

Example

If nothing else is defined in the formatting settings, the example produces the output shown below. Note the place on the far right, which is reserved for the sign for type i.

DATA text TYPE c LENGTH 20. 

SET COUNTRY 'US'. 

WRITE CONV i( '123456' ) TO text. 
cl_demo_output=>write( text ). 
WRITE CONV decfloat34( 123456 ) TO text. 
cl_demo_output=>write( text ). 
WRITE CONV f( 123456 ) TO text. 
cl_demo_output=>display( text ).
            123,456

             123,456

 1.2345600000000E+05

Character-like data types

Data objects of character-like data types are applied without formatting. With character-like data types with a fixed length (c, n), trailing blanks are cut off. The content of text strings with the type string is used in full.


Example

After the assignment has been made, the variable text contains the left-aligned character string Hello World!.

DATA text TYPE c LENGTH 20. 

WRITE 'Hello World!' TO text. 
cl_demo_output=>display( text ).

Byte-Like Data Types

Byte-like data objects are formatted in hexadecimal format, where one byte is represented by two characters.


Example

After the assignment has been made, the variable text contains the left-aligned character string 48656C6C6F20576F726C.

DATA text TYPE c LENGTH 20. 

WRITE cl_abap_conv_codepage=>create_out(  )->convert( 'Hello World!' ) 
  TO text. 

cl_demo_output=>display( text  ).

Date Types and Time Types

The content of a date field or time field of type d or t or the content of a time stamp field of type utclong is formatted in accordance with the rules for date and time formats below. Here, the content is not checked for the character-like types d and t .


Example

If nothing else is defined in the formatting settings, the example produces the output shown below.

DATA text TYPE c LENGTH 10. 

SET COUNTRY 'US'. 

WRITE CONV d( 'XXXXXXXX' ) TO text. 
cl_demo_output=>write( text ). 
WRITE CONV t( 'XXXXXX' ) TO text. 
cl_demo_output=>display( text ).
XX/XX/XXXX

XX:XX:XX

Number, Date, and Time Formats

With one exception, numbers, date formats, and time formats are defined by the current formatting setting of the language environment, which can be configured using SET COUNTRY.

Number Output

The predefined number format for the output of numeric fields (which decimal and thousands separators are selected) is defined by the current formatting setting. The formats available correspond to the country-specific number formats. Thousands separators are inserted only if there is enough length available.


Example

If nothing else is defined in the formatting settings, the example produces the output shown below.

DATA text TYPE c LENGTH 10. 

SET COUNTRY 'US'. 
WRITE CONV i( '1000000' ) TO text. 
cl_demo_output=>write( text ). 
SET COUNTRY 'DE'. 
WRITE CONV i( '1000000' ) TO text. 
cl_demo_output=>display( text ).
1,000,000

1,000,000

Date Output

The predefined date format for the output of date fields of type d is defined by the current formatting setting. The formats available correspond to the country-specific date formats. Separators are inserted only if there is enough length available. A date field is formatted regardless of its content.


Example

If nothing else is defined in the formatting settings, the example creates the format shown below.

DATA text TYPE c LENGTH 10. 

SET COUNTRY 'US'. 
WRITE sy-datlo TO text. 
cl_demo_output=>write( text ). 
SET COUNTRY 'DE'. 
WRITE sy-datlo TO text. 
cl_demo_output=>display( text ).
04/04/2019

04.04.2019

Time Output

Time fields of type t are by default always displayed in 24-hour format with colons as separators. Separators are inserted only if there is enough length available. To use a 12-hour format defined in the current formatting settings, the addition ENVIRONMENT TIME FORMAT must be used. A time field is formatted regardless of its content.


Example

The example produces the format shown below.

DATA: text1 TYPE c LENGTH 8, 
      text2 TYPE c LENGTH 6. 

WRITE sy-timlo TO text1. 
cl_demo_output=>write( text1 ). 
WRITE sy-timlo TO text2. 
cl_demo_output=>display( text2 ).
13:07:35

130.735

Time Stamp Output

The output of time stamp fields with the time stamp type utclong and time stamps in packed numbers where the addition TIME ZONE is specified is constructed from a date and time specified in their current formats and separated by a blank.

  • The date format is taken from the current formatting setting both for time stamp fields with the type utclong and for time stamps in packed numbers.
  • The time format is determined as follows:
  • The time format is taken from the current formatting settings for time stamp fields with the type utclong. In the case of initial time stamp fields, the target range is padded with blanks. A time stamp field whose value is greater than or equal to the last second or is between "9999-12-31 23:59:59.0000000” and "9999-12-31 23:59:59.9999999" is represented as "9999-12-31 23:59:59.9999999".
  • In the case of time stamps in packed numbers, the time format is always the 24 hour format with colons as separators. The 12 hour time format is not possible. In front of second fractions, the decimal separator is always a comma (,). An initial packed number is viewed as invalid by the addition TIME ZONE and its output is "*00.00.0000 00:00:00.000000.


Notes

  • The addition TIME ZONE must be specified for time stamp fields with the type TIME ZONE.

  • Time stamps in packed numbers for which the addition TIME ZONE is not specified are handled as data type p.

Example

If nothing else is defined in the formatting settings, the example creates the format shown below.

DATA text TYPE c LENGTH 30. 

DATA(ts)   = utclong_current( ). 
DATA(tspl) = cl_abap_tstmp=>utclong2tstmp( ts ). 

SET COUNTRY 'DE'. 
WRITE ts TO text TIME ZONE 'UTC'. 
cl_demo_output=>write( text ). 
WRITE tspl TO text TIME ZONE 'UTC'. 
cl_demo_output=>write( text ). 

SET COUNTRY 'US'. 
WRITE ts TO text TIME ZONE 'UTC'. 
cl_demo_output=>write( text ). 
WRITE tspl TO text TIME ZONE 'UTC'. 
cl_demo_output=>display( text ).
04.04.2019 12:42:48.2006610

04.04.2019 12:42:48.2006610

04/04/2019 12:42:48.2006610

04/04/2019 12:42:48.2006610