‎2008 May 31 9:44 AM
Hi Gurus,
I have an internal table with values country code and current price and using a selection screen i get a price, now i need to pick a value from the table which is nearest to the one in the screen. How do i achieve this logic? All help regarding this is very much appreciated.
Thanks,
‎2008 May 31 9:59 AM
you can do like this..
parameters : valin type p decimals 2.
data:begin of itab occurs 0,
val type p decimals 2,
end of itab.
itab-val = valin.
append itab.
sort itab by val descending.
regards,
venkat
‎2008 May 31 10:26 AM
give 10 as input and execute this program
parameters:valin type p.
data:begin of itab occurs 0,
val type p decimals 2,
end of itab.
itab-val = '10.13'.
append itab.
itab-val = '14.13'.
append itab.
itab-val = '9.13'.
append itab.
itab-val = '4.13'.
append itab.
itab-val = '22.13'.
append itab.
sort itab descending.
loop at itab where val <= valin.
write itab-val.
exit.
endloop.
‎2008 May 31 10:58 AM
give 10 as input and execute this program
parameters:valin type p.
data:begin of itab occurs 0,
val type p decimals 2,
end of itab.
itab-val = '10.13'.
append itab.
itab-val = '14.13'.
append itab.
itab-val = '9.13'.
append itab.
itab-val = '4.13'.
append itab.
itab-val = '22.13'.
append itab.
sort itab descending.
loop at itab where val <= valin.
write itab-val.
exit.
endloop.
Hi Keshu,
I need to find the nearest , i.e., if there is 9.8 in the table and 10.11 in the table and the value i enter is 10, then my program should pick up 10.11 as nearest value.
‎2008 May 31 11:41 AM
this will work....donot forget to reward
DATA:leval TYPE p DECIMALS 2.
DATA:geval TYPE p DECIMALS 2.
DATA:final_val1 TYPE p DECIMALS 2.
DATA:final_val2 TYPE p DECIMALS 2.
DATA:BEGIN OF itab OCCURS 0,
val TYPE p DECIMALS 2,
END OF itab.
PARAMETERS:valin TYPE p.
itab-val = '10.13'.APPEND itab.
itab-val = '14.13'.APPEND itab.
itab-val = '9.8'.APPEND itab.
itab-val = '4.13'.APPEND itab.
itab-val = '10.11'.APPEND itab.
SORT itab DESCENDING BY val.
LOOP AT itab WHERE val <= valin.
leval = itab-val.
EXIT.
ENDLOOP.
SORT itab ASCENDING BY val.
LOOP AT itab WHERE val >= valin.
geval = itab-val.
EXIT.
ENDLOOP.
final_val1 = ABS( valin - leval ).
final_val2 = ABS( valin - geval ).
IF final_val1 < final_val2.
WRITE leval.
ELSEIF final_val2 < final_val1.
WRITE geval.
ELSEIF final_val1 = 0 AND final_val2 <> 0.
WRITE leval.
ELSEIF final_val2 = 0 AND final_val1 <> 0.
WRITE geval.
ENDIF.