How to get Last digit of number

前端 未结 8 1294
不思量自难忘°
不思量自难忘° 2021-02-12 11:09

How to extract last(end) digit of the Number value using jquery. because i have to check the last digit of number is 0 or 5. so how to get last digit after decimal point

<
8条回答
  •  别那么骄傲
    2021-02-12 11:21

    Try this one:

    var test = 2354.55;
    var lastone = +test.toString().split('').pop();
    
    console.log("lastone-->", lastone, "<--typeof", typeof lastone);
    
    // with es6 tagged template and es6 spread
    let getLastDigit = (str, num)=>{
      return num.toString();
    };
    let test2 = 2354.55;
    let lastone2 = +[...getLastDigit`${test2}`].pop();
    
    console.log("lastone2-->", lastone2, "<--typeof", typeof lastone2);


    Updates with ES6/ES2015:

    We can use tagged template in such case as numbers are not iterable. So, we need to convert the number to a string representation of it. Then just spread it and get the last number popped.

提交回复
热议问题