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 float

Former Member
0 Likes
6,415

It seems extremely rudimentary but I haven't been able to find an answer yet.

I would like to pass in a string representing a human-readable floating point (ie, non IEEE 754) and get its value.

A straight up assignment of a string variable into a f variable doesn't work for thousand dividers. We would also need it for every scenarios:

123.456

123,456

123456

123456,789

123.456,789

123,456.789

all should be valid inputs and the resolution should be dependent on system setting for number formats.

I would like a built-in ABAP call with no manual processing. I'm sure this problem has been encountered thousands of times and solved thousands of times. No point reinventing the wheel.

1 ACCEPTED SOLUTION
Read only

Clemenss
Active Contributor
0 Likes
2,796

Hi Xiao Yu,

use function module RS_CONV_EX_2_IN. In parameters, you must give a table name and field of type f or P with enough decimals. Can use any table/structure or define one yourself.

The FM will regard all applicable country and user settings. If conversion fails you get a qualified exception via sy-subrc.

This FM is used by SAP for input conversion in editable ALV. ALV cells are 2555 character fields, so it should work for you.

Regards,

Clemens

5 REPLIES 5
Read only

Former Member
0 Likes
2,796

Tested Code

Output as below

String : 123,456,789,123.456

String : 123456789123.456

Float : 1.2345678912345599E+11

DATA v_str TYPE STRING VALUE '123,456,789,123.456'.
DATA v_flt TYPE F.

WRITE: / 'String :', v_str. "With Commas
REPLACE ALL OCCURRENCES OF ',' IN v_str WITH ''.
WRITE: / 'String :', v_str. "Without Commas
CATCH SYSTEM-EXCEPTIONS ARITHMETIC_ERRORS = 1
                        CONVERSION_ERRORS = 2.
  MOVE v_str TO v_flt.
ENDCATCH.
WRITE: / 'Float  :', v_flt. "Float value

Read only

0 Likes
2,796

Hi Priyanka,

Thanks for the response. The thing is we cannot be certain that ',' are in fact thousand separators and the '.' is the decimal point. It depends on system settings (ie, the Europeans have it the other way around)

I'd be surprised if there isn't a built-in function or class that does it while taking system settings into account.

Read only

0 Likes
2,796

So we are sure that we will have '.' or ',' in which ever the currency format. Remove all ',' and '.' in the string

"Contiunation to my previous helpful answere ->

So replace all ',' and '.' as said in my previous answere. and note here is we will have decimals 2. So make the final string to have 2 decimals and convert to float.

Read only

Clemenss
Active Contributor
0 Likes
2,797

Hi Xiao Yu,

use function module RS_CONV_EX_2_IN. In parameters, you must give a table name and field of type f or P with enough decimals. Can use any table/structure or define one yourself.

The FM will regard all applicable country and user settings. If conversion fails you get a qualified exception via sy-subrc.

This FM is used by SAP for input conversion in editable ALV. ALV cells are 2555 character fields, so it should work for you.

Regards,

Clemens

Read only

Former Member
0 Likes
2,796

Hi Clemens,

This is great! It definitely looks like what we need. We'll give it a try now. Thanks a lot!!