Number between .....

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Number between .....

Post by RossG » Wed Dec 02, 2015 9:59 pm

Is there a way to determine whether a number is within
a specified range other than writing

if theVar is > .... then
if theVar is < ... then ?
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: Number between .....

Post by Martin Koob » Wed Dec 02, 2015 10:12 pm

You could have the two conditions in one line using and like this

Code: Select all

 if tVar > 10 and tVar < 20 then 
-- do something
end if
Martin

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10319
Joined: Wed May 06, 2009 2:28 pm

Re: Number between .....

Post by dunbarx » Wed Dec 02, 2015 10:48 pm

Or, cuter, (from the dictionary)

max(lowValue,min(myValue,highValue))

Though this gives a different sort of answer.

Craig Newman

TorstenHolmer
Posts: 58
Joined: Mon Oct 28, 2013 1:23 pm

Re: Number between .....

Post by TorstenHolmer » Sat Nov 07, 2020 11:23 am

If you need the comparison more than once in your code, it is better to create a custom handler :-)

numberWithin(2,2,4) --> true
numberWithin(5,2,4) --> false

Code: Select all

function numberWithin pNumber, pMin, pMax
   
   if pNumber >= pMin and pNumber <= pMax then
      return true
   else
      return false
   end if
   
end numberWithin

Happy Coding
Torsten

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Number between .....

Post by Klaus » Sat Nov 07, 2020 11:55 am

A little late for the party, Torsten... :D

Post Reply