检查整数是否以0结尾

时间:2016-05-09 03:08:13

标签: javascript if-statement

如何创建if语句以检查整数是否以0结尾?

例如,我想要这样的if语句:

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
}

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:4)

if(/0$/.test(number)) {
  /* it ends in a 0 */
}
else {
  /* it doesn't */
}

答案 2 :(得分:4)

//使用模数

var test1 = 110

if(test1%10 == 0){ } //数字以零结尾

答案 3 :(得分:4)

      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");
      }
  

示例:

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

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