Count Number Of Digits In A Number Javascript Code Example


Example 1: digit count in javascript


function digitCount(num) {
if(num === 0 ) return 1
return Math.floor(Math.log10(Math.abs(num))) + 1
}

Example 2: javascript count digits


var length = (number + '').replace('.', '').replace('-', '').length;  // for numbers that can be floats or negatives

Example 3: get number right of the dot length javascript


var x = 23.453453453;
x.countDecimals(); // 9

var countDecimals = function (value) {
if(Math.floor(value) === value) return 0;
return value.toString().split(".")[1].length || 0;
}

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable