Check if integer ends with 0

10,755

Solution 1

      var test=107;
      var isEndsWithZero =test%10; 
      if(isEndsWithZero!==0)
      {
        //Ends With 1-9,Do something
        alert("Ends With 1 to 9");
      }
      else
      {
        //Ends With Zero ,Do something
        alert ("ends with zero");
      }

Example:

test=107;
isEndsWithZero=107%10 //7
Else part will get executed

JSFiddle Link :https://jsfiddle.net/nfzuaa44/

Solution 2

if (test % 10) {
    // does not end with 0
} else {
    // ends with 0
}

Solution 3

//Use Modulus

var test1=110

if(test1 % 10 == 0){ }// Number Ends with a zero

Solution 4

if(/0$/.test(number)) {
  /* it ends in a 0 */
}
else {
  /* it doesn't */
}
Share:
10,755
Anson Aştepta
Author by

Anson Aştepta

Updated on June 25, 2022

Comments

  • Anson Aştepta
    Anson Aştepta almost 2 years

    How may I create an if-statement in order to check if an integer ends with 0?

    For example, I'd like to have an if-statement like this:

    var test = 107; //107 is an example it'should some unknown number
    
    if(test is xx1 - xx9){
      //do something
    }
    else if(test is xx0){
      //do something
    }
    
  • Kunal Grover
    Kunal Grover almost 8 years
    Depends on the language that you are using, Python wouldn't have a problem with (test % 10), but Java would.