ā2013 Apr 29 6:27 AM
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
ā2013 Apr 29 4:11 PM
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
ā2013 Apr 29 6:47 AM
but if the value is 12.6999 means the value is we get the output as 12.70Really? Mathematically this makes no sense https://en.wikipedia.org/wiki/0.999.... Perhaps you should push back on this "requirement".
ā2013 Apr 29 8:46 AM
ā2013 Apr 29 12:18 PM
That's true. But I'm doubtful of business logic that really requires trimming in this way!
ā2013 Apr 29 7:01 AM
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...
ā2013 Apr 29 8:29 AM
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.
ā2013 Apr 29 4:11 PM
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
ā2013 May 03 7:25 AM
ā2013 May 03 7:25 AM
HI SATISH,
IT WORKS FINE.THANKS FOR THE SUPPORT
REGARDS,
RANJITH.
ā2013 Apr 30 7:28 AM
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