Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Created August 26, 2015 07:48
Show Gist options
  • Save juliengdt/95fd31457c765d5e9535 to your computer and use it in GitHub Desktop.
Save juliengdt/95fd31457c765d5e9535 to your computer and use it in GitHub Desktop.
To check if a number is between a range in Swift
// To check if a number is between a range, don't do
if number >=0 && number <= 100 {
}
// Use range and news operators instead :
if 0...100 ~= number {
}
@azakordonets
Copy link

@angelopino :

if 1...100 ~= number {
}

@azakordonets
Copy link

Also, if you want to check that 100 is not included :

if 0..<100 ~= number {
}

@jmitch22
Copy link

how would i check if a number is between 1 and 12(inclusive)?

@mohammed-abuamra
Copy link

mohammed-abuamra commented Jun 8, 2018

How i can check if number from 1 to 4 and else if number from 3 to 0 ?

@delixion
Copy link

I can check "Integer" number > 0 and < 100, by 1..<100 ~= number
but how would I check "float" number > 0 and < 1 ? (excluding 0 and 1)

@delixion
Copy link

@jmitch22 1...12 ~= number

@lekysma
Copy link

lekysma commented Apr 29, 2020

what about if number is greater than 40 but lesser than 80 ? >=40..<80 did not seem to work

@richkolasa
Copy link

I prefer (40..<80).contains(number)

@scott-lydon
Copy link

Thanks!

@pkc456
Copy link

pkc456 commented Jun 24, 2020

Thanks

Copy link

ghost commented Nov 7, 2020

THANKSSS

@ramonfsk
Copy link

thanks bro!

@AMBIENTE1
Copy link

Please, how would I check if Int(number) is between -1999 and -1001 ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment