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: Klaus, FourthWorld, heatherlaine, kevinmiller

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

Number between .....

Post by RossG »

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 »

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: 10509
Joined: Wed May 06, 2009 2:28 pm

Re: Number between .....

Post by dunbarx »

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 »

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: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Number between .....

Post by Klaus »

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