‎2005 Dec 08 4:06 PM
I want to convert a string '127,280.01' to decimal is there any function module that i can use?
Thanx in advance
‎2005 Dec 08 4:12 PM
Jose, try this.
report zrich_0001
no standard page heading.
data: s type string.
data: p Type p decimals 2.
s = '127,280.01'.
translate s using ', '.
condense s no-gaps.
clear p.
p = p + s.
write:/ p.
Here we are removing the , for the thousands separator, condensing and just adding the value to a P field.
Regards,
Rich Heilman
‎2005 Dec 08 4:12 PM
Jose, try this.
report zrich_0001
no standard page heading.
data: s type string.
data: p Type p decimals 2.
s = '127,280.01'.
translate s using ', '.
condense s no-gaps.
clear p.
p = p + s.
write:/ p.
Here we are removing the , for the thousands separator, condensing and just adding the value to a P field.
Regards,
Rich Heilman
‎2005 Dec 08 4:14 PM
You can directly assign string variable to packed integer with decimals (Type P) . But the string you have defined contains Comma (,) that is the reason it is not able to convert. use string functions to eliminate the comma from the string and assign to the numeric value.
‎2005 Dec 08 4:14 PM
‎2005 Dec 08 4:17 PM
Use this
HRCM_STRING_TO_AMOUNT_CONVERT
STRING 123,34.567
DECIMAL_SEPARATOR .
THOUSANDS_SEPARATOR ,
WAERS leave it balnk
you can convert to DEcimal
regards
vijay
‎2005 Dec 08 4:19 PM
hi joes try this ..FM
<b>HRCM_STRING_TO_AMOUNT_CONVERT</b>
STRING 123,34.567
DECIMAL_SEPARATOR .
THOUSANDS_SEPARATOR ,
WAERS (BALNK)
‎2005 Dec 08 4:20 PM
‎2005 Dec 08 4:22 PM
u have string as 127,280.01 This is already in decimal notation.
Just u need to move the char value to integer value.
data : v1 type p decimals 2.
move string to v1.
This solves.
‎2005 Dec 08 5:05 PM
Hi
It's very easy operation, so I don't know if there are some fm.
You only need to move your string to decimal number by MOVE comand.
Before moving the string you have to delete the virgola, because the dump occurs:
So:
DATA: NUMBER_C(20) VALUE '127,280.01',
NUMBER TYPE P DECIMALS 2.
Here NUMBER_C is 127,280.01
DO.
REPLACE ',' WITH SPACE INTO NUMBER_C.
IF SY-SUBRC <> 0. EXIT. ENDIF.
CONDENSE NUMBER_C NO-GAP.
ENDDO.
Now NUMBER_C is 127280.01
MOVE NUMBER_C TO NUMBER.
Max
Message was edited by: max bianchi
‎2005 Dec 08 5:27 PM
Hi Joes
Check the Function Module once...
vijay
Message was edited by: Vijay Babu Dudla
‎2005 Dec 08 5:30 PM
As Vijay is suggesting, the function module works too.
report zrich_0001
no standard page heading.
data: s type string.
data: p type p decimals 2.
s = '1,765,654.25'.
call function 'HRCM_STRING_TO_AMOUNT_CONVERT'
exporting
string = s
decimal_separator = '.'
thousands_separator = ','
importing
betrg = p
exceptions
convert_error = 1
others = 2.
write:/ p.
REgards,
Rich Heilman
‎2013 May 23 6:02 PM
Hi Rich,
where can I find the function "'HRCM_STRING_TO_AMOUNT_CONVERT'.
In System of my customer I can't find such function. I need to convert a CHAR field into a DEC.
Regards,
Vjola
‎2022 Oct 20 8:23 AM
Hi,
Here is my solution.
And just call that static method like
zcl_utils=>convert_str_to_dec(
EXPORTING
value = lv_tmp1
IMPORTING
result = lv_tmp1
).
CLASS zcl_utils DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
"Operation return status
CONSTANTS c_stat_success TYPE char1 VALUE 'S' ##NO_TEXT.
CONSTANTS c_stat_warning TYPE char1 VALUE 'W' ##NO_TEXT.
CONSTANTS c_stat_error TYPE char1 VALUE 'E' ##NO_TEXT.
CONSTANTS c_stat_info TYPE char1 VALUE 'I' ##NO_TEXT.
"Status Text
TYPES: BEGIN OF gty_status,
status TYPE char1,
status_text TYPE char255,
END OF gty_status.
CLASS-METHODS convert_str_to_dec
IMPORTING
VALUE(value) TYPE clike
VALUE(format) TYPE char30 OPTIONAL
EXPORTING
result TYPE clike
status TYPE gty_status.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS ZCL_UTILS IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_UTILS=>CONVERT_STR_TO_DEC
* +-------------------------------------------------------------------------------------------------+
* | [--->] VALUE TYPE CLIKE
* | [--->] FORMAT TYPE CHAR30(optional)
* | [<---] RESULT TYPE CLIKE
* | [<---] STATUS TYPE GTY_STATUS
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD convert_str_to_dec.
"Expected input, sample £10.000,52
"Output should be 10000.52
DATA: exref TYPE REF TO cx_root.
TRY.
REPLACE ALL OCCURRENCES OF REGEX '[^(0-9.,)]' IN value WITH ''.
IF value IS NOT INITIAL.
IF format IS INITIAL.
format = value.
ENDIF.
FIND ALL OCCURRENCES OF '.' IN format MATCH COUNT DATA(dot_count).
FIND ALL OCCURRENCES OF ',' IN format MATCH COUNT DATA(comma_count).
IF dot_count > 1.
REPLACE ALL OCCURRENCES OF SUBSTRING '.' IN value WITH ''.
TRANSLATE value USING ',.'.
ELSEIF comma_count > 1.
REPLACE ALL OCCURRENCES OF SUBSTRING ',' IN value WITH ''.
ELSE.
FIND FIRST OCCURRENCE OF '.' IN format MATCH OFFSET DATA(dot_place).
FIND FIRST OCCURRENCE OF ',' IN format MATCH OFFSET DATA(comma_place).
"£10.000,00 -> 10,000.00
IF dot_place IS NOT INITIAL AND comma_place IS NOT INITIAL.
IF dot_place < comma_place.
TRANSLATE value USING ',;'.
REPLACE ALL OCCURRENCES OF SUBSTRING '.' IN value WITH ''.
TRANSLATE value USING ';.'.
ELSE.
REPLACE ALL OCCURRENCES OF SUBSTRING ',' IN value WITH ''.
ENDIF.
"£10000,00
ELSEIF dot_place IS INITIAL AND comma_place IS NOT INITIAL.
TRANSLATE value USING ',.'.
ENDIF.
ENDIF.
result = value.
CONDENSE result NO-GAPS.
ENDIF.
status-status = c_stat_success.
CATCH cx_root INTO exref.
status-status = c_stat_error.
status-status_text = exref->get_text( ).
ENDTRY.
ENDMETHOD.
ENDCLASS.