Posted: Thu Oct 18, 2007 5:49 pm Post subject: Data elements conversion | Преобразование типов данных
MOVE_CHAR_TO_NUM – This is a good all-purpose FM (works with CHAR but not with STRING though). Its major advantage is that the thousands separator is not limited to a comma, like in my example above.
Code:
DATA: zchar(15) VALUE '123.456.789,12'.
DATA: znum(8) TYPE p DECIMALS 3.
CALL FUNCTION 'MOVE_CHAR_TO_NUM'
EXPORTING
chr = zchar
IMPORTING
num = znum
EXCEPTIONS
convt_no_number = 1
convt_overflow = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
WRITE: / 'error', sy-subrc.
ENDIF.
WRITE: / 'znum =', znum.
URL_ASCII_CODE_GET - Delivers ASCII code of a character. Still platform-dependent!
Code:
*ascii code for character..
DATA: c, n type i, i type i.
n = STRLEN( text ).
i = 0.
do n times.
CALL FUNCTION 'URL_ASCII_CODE_GET' "Delivers ASCII code of a character.
EXPORTING
trans_char = text+i(1) "Character to translate
IMPORTING
char_code = c. "Hex code string for character
i = i + 1.
*Perform ur logic with Char_code.
enddo.
CATS_NUMERIC_INPUT_CHECK – this one has a very limited application IMHO. It does not convert character fields to numeric fields. Basically all it does it checks if the field is numeric, removes the thousands separator and, if there is a negative sign, moves it to the end. It could not handle the number '+107,400.99' and threw a "not numeric" exception.
RS_CONV_EX_2_IN_DTEL - Converts the contents of a field in external format (INPUT_EXTERNAL) into internal format (OUTPUT_INTERNAL).
The parameter OUTPUT_INTERNAL must have the technical attributes of the data element specified in DTEL. The field INPUT_EXTERNAL must be a character field. It can have any length, but must not be longer than the output length of DTEL, otherwise the exception INPUT_TOO_LONG is raised.
All exceptions that relate to invalid field contents are handled using error messages.
HRCM_AMOUNT_TO_STRING_CONVERT – this FM can be used for the conversion of amounts since it takes the currency into account. For the simple string to number conversion it is a bit too bulky, in my opinion.
It seems that ABAP is one of the few languages that does not have an operator to validate whether the field is numeric or not. In the system where most of the key fields (VBELN, MATNR, etc.) are CHAR but usually contain only numbers an IS NUMERIC operator would come in handy, don’t you think?
Oh well, as Mr. Zorg used to say: "You want something done, do it yourself!". And so I started this quest with a simple task to find a way to check whether the field is a number. I’m not going to lie to you – the idea to define a constant with numbers only and to use IF ... CO... was stolen from one of the SAP programs. Here is my first test program:
Code:
CONSTANTS: numbers(10) VALUE '1234567890'.
DATA: test(10).
test = '123ABC'.
PERFORM test_check USING test.
test = '123'.
PERFORM test_check USING test.
FORM test_check USING p_test.
IF p_test CO numbers.
WRITE: / p_test , ' contains only numbers'.
ELSE.
WRITE: / p_test , ' contains alpha characters'.
ENDIF.
ENDFORM. " test_check
But the result surprised me:
123ABC contains alpha characters
123 contains alpha characters
What?! Since when 123 is not a number?! Well, obviously, sometimes the owls are not what they seem. Here is what the documentation says:
CO (Contains Only):
c1 contains only characters from the string c2.
If c1 or c2 is of type C, the comparison takes into account the full length of the field, including blanks at the end.
Doh! Damn SAP with their blanks... OK, I can work around this. Not sure if there is a better way to do this but I found my own method to pad a number with leading zeroes by using SHIFT and TRANSLATE operators. Here is my test number two (I changed only the test_check routine:
Code:
FORM test_check USING VALUE(p_test).
SHIFT p_test RIGHT DELETING TRAILING ' '.
TRANSLATE p_test USING ' 0'.
IF p_test CO numbers.
WRITE: / test , ' contains only numbers'.
ELSE.
WRITE: / test , ' contains alpha characters'.
ENDIF.
ENDFORM. " test_check
The result:
123ABC contains alpha characters
123 contains only numbers
Tadah! Note that if you don’t use FORM ... USING VALUE... then the variable test will be converted to ‘0000000123’. (Boy, I feel so smart right now. )
OK. But this piece of magic has actually very limited application. What if the field contains characters like ‘+’, ‘,’ or ‘.’, which can also be a part of the number? Coincidentally, on one of the SAP forums someone has posted a question how to convert a string (for example '107,400.99') into an integer. Since I was already on this numeric quest, I continued in this new direction. Here is what I came up with:
Code:
DATA: input_string TYPE string,
output_integer TYPE i.
IF sy-subrc = 0.
WRITE: output_integer.
ELSE.
MESSAGE 'Not a number' TYPE 'E'.
ENDIF.
The result: 107,401 (since its type I the decimals have been rounded). This piece of code also works when input_string is type CHAR and output can actually be any numeric type. It works with NUMC, P (with or without DECIMALS) and currency types equally well. Also the input string can have plus and minus sign and it can be upfront or at the end of the number – it will still work. The standard ABAP type conversion will take care of converting decimals and the sign, so we only have to remove the thousand separator (‘,’ in this case). CATCH clause will catch an exception if the string contains any other characters (e.g. letters).
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.