Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Convert string to decimal

Former Member
0 Likes
38,913

I want to convert a string '127,280.01' to decimal is there any function module that i can use?

Thanx in advance

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
13,502

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

12 REPLIES 12
Read only

RichHeilman
Developer Advocate
Developer Advocate
13,503

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

Read only

Former Member
0 Likes
13,502

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.

Read only

Former Member
0 Likes
13,502

try using FM CONVERT_STRING_TO_INTEGER.

Read only

Former Member
0 Likes
13,502

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

Read only

Former Member
0 Likes
13,502

hi joes try this ..FM

<b>HRCM_STRING_TO_AMOUNT_CONVERT</b>
STRING                          123,34.567   
DECIMAL_SEPARATOR               .               
THOUSANDS_SEPARATOR             ,               
WAERS         (BALNK)                                 

Read only

Former Member
0 Likes
13,502

please reward if you got the solution...

vijay

Read only

Former Member
0 Likes
13,502

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.

Read only

0 Likes
13,502

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

Read only

Former Member
0 Likes
13,502

Hi Joes

Check the Function Module once...

vijay

Message was edited by: Vijay Babu Dudla

Read only

0 Likes
13,502

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

Read only

0 Likes
13,502

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

Read only

beyhan_meyrali
Active Participant
13,502

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.