Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Created August 26, 2015 07:48
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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 {
}
@stevekim0417
Copy link

Thanks.

@alimir1
Copy link

alimir1 commented Oct 18, 2016

thanks! 👍

@gracietti
Copy link

thanks!

@mding5692
Copy link

Appreciate this

@Fengson
Copy link

Fengson commented Aug 28, 2017

How would I check if number is > 0 and < 100 (excluding 0 and 100) using this?

@ibrahimkteish
Copy link

ibrahimkteish commented Oct 24, 2017

@Fengson use 1..<100

@ssowri1
Copy link

ssowri1 commented Nov 6, 2017

Thank you! It saved my time.

@vampistano
Copy link

how to compare two int are equal or not ...

@angelopino
Copy link

How would I check if number is > 0 and <= 1 (excluding 0) using this?

@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