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

Finding the nearest value.

former_member216668
Participant
0 Likes
1,420

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,

4 REPLIES 4
Read only

Former Member
0 Likes
748

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

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
748

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.

Read only

0 Likes
748

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.

Read only

0 Likes
748


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.