How to get Last digit of number

前端 未结 8 1244
不思量自难忘°
不思量自难忘° 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:33

    This worked for us:

    var sampleNumber = 123456789,
      lastDigit = sampleNumber % 10;
    console.log('The last digit of ', sampleNumber, ' is ', lastDigit);

    Works for decimals:

    var sampleNumber = 12345678.89,
      lastDigit = Number.isInteger(sampleNumber) ? sampleNumber % 10
        : sampleNumber.toString().slice(-1);
    console.log('The last digit of ', sampleNumber, ' is ', lastDigit);

    Click on Run code snippet to verify.

提交回复
热议问题