最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if integer ends with 0 - Stack Overflow

programmeradmin6浏览0评论

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
}

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
}
Share Improve this question edited May 10, 2016 at 0:46 Pang 10.1k146 gold badges86 silver badges124 bronze badges asked May 9, 2016 at 3:08 Anson AşteptaAnson Aştepta 1,1452 gold badges14 silver badges39 bronze badges 3
  • yes, i should better ask it this way, i have updated my question – Anson Aştepta Commented May 9, 2016 at 3:12
  • 2 if (n % 10) /* doesn't end in zero */ – RobG Commented May 9, 2016 at 3:13
  • stackoverflow.com/questions/20433364/… – Venkat Commented May 13, 2016 at 8:30
Add a comment  | 

4 Answers 4

Reset to default 7
if (test % 10) {
    // does not end with 0
} else {
    // ends with 0
}
      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/

//Use Modulus

var test1=110

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

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

评论列表(0)

  1. 暂无评论