In SAP ABAP, converting an integer to a string (or character) can be done in multiple ways. Here are the best methods:
🔹 1. Implicit Conversion (Direct Assignment)
ABAP allows automatic conversion from I (integer) to STRING or C (character).
Example
DATA: lv_int TYPE I VALUE 123, lv_str TYPE STRING. " Dynamic-length string
lv_str = lv_int. " Implicit conversion
WRITE: lv_str. " Output: 123📌 Works well for most cases and keeps the code clean.
🔹 2. Using CONV (Explicit Conversion)
The CONV function explicitly converts an integer to a string.
Example
DATA: lv_int TYPE I VALUE 456, lv_str TYPE STRING.
lv_str = CONV string( lv_int ).
WRITE: lv_str. " Output: 456📌 Recommended for clarity in modern ABAP.
🔹 3. Using WRITE TO Statement
The WRITE TO statement formats the integer into a character variable.
Example
DATA: lv_int TYPE I VALUE 789, lv_char TYPE C LENGTH 10. " Fixed-length character
WRITE lv_int TO lv_char.
WRITE: lv_char. " Output: '789 ' (right-padded)📌 Good for formatted output, but be careful with fixed-length padding.
🔹 4. Using && Concatenation (Quick Inline Conversion)
Concatenating an integer with an empty string converts it to a string.
Example
DATA: lv_int TYPE I VALUE 1000, lv_str TYPE STRING.
lv_str = lv_int && ''. " Concatenation forces conversion
WRITE: lv_str. " Output: 1000📌 Simple and effective for quick conversions.
📝 Summary: Best Methods for Integer to String Conversion
| Method | Best For | Example |
|---|---|---|
| Implicit Assignment | Simple cases | lv_str = lv_int. |
CONV Function | Explicit conversion (modern ABAP) | lv_str = CONV string( lv_int ). |
WRITE TO Statement | Formatted output (fixed-length) | WRITE lv_int TO lv_char. |
Concatenation (&&) | Quick inline conversion | lv_str = lv_int && ''. |
Would you like an example using internal tables or formatted output? 🚀