21. December 2022 – EXACT – Lossless Conversion

Probably everyone has encountered this at some point. The value in the target field has been truncated by an assignment or an undesired rounding has occurred. The lossless operator EXACT can be used to check lossless assignments or lossless calculations. If lossless transfer is not possible, an exception is raised.

 

Example 1 – Lossless assignment

 

Here is a very simple example. The program checks if there is a data loss when assigning a string into a character (length 5) field:

 

DATA l_char_5 TYPE c LENGTH 5.
DATA l_string TYPE string.

l_string = `ABAPventcalendar`.
out->write( `Source value: ` && l_string ).
l_char_5 = l_string.
out->write( `Assignment with data loss: ` && l_char_5 ).

TRY.
   l_char_5 = EXACT #( l_string ).
   CATCH cx_sy_conversion_data_loss INTO DATA(exception).
      out->write( exception->get_text( ) ).
ENDTRY.

 

And here is the output

 

Source value: ABAPventcalendar
Assignment with data loss: ABAPD
Data loss occurred when converting ABAPventcalendar

 

Example 2 – Unwanted rounding

 

Sometimes rounding is wanted, but if not, this can also be prevented with EXACT as follows.

 

DATA l_int TYPE i.
DATA l_dec_9_2 TYPE p LENGTH 9 DECIMALS 2.

l_dec_9_2 = `1234.56`.
l_int = l_dec_9_2.
out->write( `Source value: ` && l_dec_9_2 ).
out->write( `Assignment with data loss: ` && l_int ).

TRY.
   l_int = EXACT #( l_dec_9_2 ).
   CATCH cx_sy_conversion_rounding INTO DATA(exception).
      out->write( exception->get_text( ) ).
ENDTRY.

 

And here again the output

 

Source value: 1234.56
Assignment with data loss: 1235
Conversion or Calculation Could Not Be Executed Exactly; Rounding Necessary

 

Example 3 – Invalid source value

 

This is a nice practical example. Here we try to transfer a string into an implicit generated numeric field. But only if it can be transferred lossless.

 

TYPES: typ_numtext TYPE n LENGTH 20.

DATA l_string TYPE string.

l_string = `20 Apples`.

TRY.
   DATA(l_numc) = EXACT typ_numtext( l_string ).
   CATCH cx_sy_conversion_error INTO DATA(exception).
      out->write( exception->get_text( ) ).
ENDTRY.

 

The output

 

The argument ’20 Apples‘ cannot be interpreted as a number

 

Example 4 – CORRESPONDING #( … )

 

The addition can also be used in CORRESPONDING as you can see in the following example. The target field field1 of structure l_target is too short.

 

types: begin of typ_source,
   field1 type string,
   field2 type string,
end of typ_source.

types: begin of typ_target,
   field1 type c length 10,
   field2 type string,
end of typ_target.

data l_source type typ_source.
data l_target type typ_target.

l_sourcefield1 = `ABAPsNotDead`.
l_sourcefield2 = `Value 2`.

try.
   l_target = corresponding #( exact l_source ).
   CATCH cx_root INTO data(exception).
      out->write( |{ exception->kernel_errid } { exception->get_text( ) }| ).
ENDTRY.

 

Result

 

CONVT_DATA_LOSS Data loss occurred when converting ABAPsNotDead

 

Additional information

 

Johann Fößleitner, Cadaxo GmbH LinkedInTwitterpeople.sap.com

cadaxo GmbH