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

Processing on the numerical operations.

Ranjith_D_Reddy
Participant
0 Likes
1,265

After passing the material number into a customized function module  I am getting the output value in numerical format.

After getting that result I need to do the following operation on it

If the output has more than 2 decimal places then the value must be trimmed

If the output has 2 decimal places then the value must be showed alike

Here are the samples

4.6 % should be converted to 4.60 %

43 % should be converted to 43.00 %

16.325 % should be converted to 16.32 %

By using below code.Let variable be lv_variable.

data: lv_var type p decimals 2.

lv_var = lv_variable.

it worked only 1f we will pass the values like 12.22,3.65,2.5555

but if the value is 12.6999 means the value is we get the output as 12.70,But I need to get the value as 12.69 only but not 12.70

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,178

Hi Ranjith,

The below code will surely help to solve your problem.

DATA:

       lv_str       TYPE string,

       lv_num1   TYPE string,

       lv_num2   TYPE string,

       lv_len       TYPE i.

**** Copy your value into lv_str here ****

SPLIT lv_str AT '.' INTO lv_num1 lv_num2.

lv_len = STRLEN( lv_num2 ).

CASE lv_len.

   WHEN 0.

     CONCATENATE lv_num1 '.00' INTO lv_str.

   WHEN 1.

     CONCATENATE lv_num1 '.' lv_num2 '0' INTO lv_str.

   WHEN OTHERS.

     lv_num2 = lv_num2+0(2).

     CONCATENATE lv_num1 '.' lv_num2 INTO lv_str.

ENDCASE.


In the above code first split the value and then get the length of decimal part. If the length is zero then concatenate two zero's to it. If length is one, concatenate single zero and if length is greater than one, get only two digit from the string and concatenate.

Hope this solved your issue.

Regards,

Satish

9 REPLIES 9
Read only

matt
Active Contributor
0 Likes
1,178

but if the value is 12.6999 means the value is we get the output as 12.70

Really? Mathematically this makes no sense https://en.wikipedia.org/wiki/0.999.... Perhaps you should push back on this "requirement".

Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
1,178

Hi ,

Thanks for exposing me to that Wikipedia link. That's really interesting. But, if I understand it correctly, it's applicable only in case of (infinitely) repeating decimals. In Ranjith's case, that may not be the case.

Read only

matt
Active Contributor
0 Likes
1,178

That's true. But I'm doubtful of business logic that really requires trimming in this way!

Read only

bishwajit_das
Active Contributor
0 Likes
1,178

If you want the output in the format as you mentioned.

Then treat the variable as a string.

string = 12.699999

SPLIT string AT '.' INTO string1 string2. string3 = string2+0(2).

CONCATENATE string1 '.' string3 INTO string.

Your output will be : string = 12.69

  I think this is the only solution...

Read only

Former Member
0 Likes
1,178

Hi Ranjith,

Try this code

PARAMETERS: p_menge TYPE mara-brgew.

data: lv_var type p decimals 2.

lv_var = p_menge.

if p_menge < lv_var.

   lv_var = lv_var - '0.01' .

endif.

I tried it is working fine for both of your  requirements.

Read only

Former Member
0 Likes
1,179

Hi Ranjith,

The below code will surely help to solve your problem.

DATA:

       lv_str       TYPE string,

       lv_num1   TYPE string,

       lv_num2   TYPE string,

       lv_len       TYPE i.

**** Copy your value into lv_str here ****

SPLIT lv_str AT '.' INTO lv_num1 lv_num2.

lv_len = STRLEN( lv_num2 ).

CASE lv_len.

   WHEN 0.

     CONCATENATE lv_num1 '.00' INTO lv_str.

   WHEN 1.

     CONCATENATE lv_num1 '.' lv_num2 '0' INTO lv_str.

   WHEN OTHERS.

     lv_num2 = lv_num2+0(2).

     CONCATENATE lv_num1 '.' lv_num2 INTO lv_str.

ENDCASE.


In the above code first split the value and then get the length of decimal part. If the length is zero then concatenate two zero's to it. If length is one, concatenate single zero and if length is greater than one, get only two digit from the string and concatenate.

Hope this solved your issue.

Regards,

Satish

Read only

0 Likes
1,178

This message was moderated.

Read only

0 Likes
1,178

HI SATISH,

IT WORKS FINE.THANKS FOR THE SUPPORT

REGARDS,

RANJITH.

Read only

arindam_m
Active Contributor
0 Likes
1,178

Hi,

Please check the following code it will suffice your requirement:

DATA: offset TYPE i,
           lv_var1 TYPE char10.

lv_var1 = '12.6999'.

IF lv_var1 CA ',.'.
  offset = sy-fdpos + 1.
  TRANSLATE lv_var1+offset(2) USING ' 0'.
  CONCATENATE lv_var1(offset) lv_var1+offset(2) INTO lv_var1.
ELSE.
  CONCATENATE lv_var1 '.' INTO lv_var1.
  IF lv_var1 CA ',.'.
    offset = sy-fdpos + 1.
    TRANSLATE lv_var1+offset(2) USING ' 0'.
    CONCATENATE lv_var1(offset) lv_var1+offset(2) INTO lv_var1.
  ENDIF.
ENDIF.

The value of the lv_var1 is considered as '12.6999'. please change this to test out the various cases as mentioned by you.

Case 1: 4.6 % should be converted to 4.60 %

Case 2:43 % should be converted to 43.00 %

Case 3:16.325 % should be converted to 16.32 %

Hope this helps

Cheers