xxxxxxxxxx
var num = 1024,
str = num.toString(),
len = str.length;
console.log(len);
xxxxxxxxxx
let a = 123
// changing to string
console.log(`${a}`.length) // 3
// without changing to string
Math.ceil(Math.log(a + 1) / Math.LN10) // 3
xxxxxxxxxx
export function numberLength(number) {
let length = 0;
let n = Math.abs(number);
do {
n /= 10;
length++;
} while (n >= 1);
return length;
}
export default numberLength;
xxxxxxxxxx
//This is correct but...
a = 123456
a.toString().length // x is a number
console.log(a.toString().length) //6
//Remeber that if the number begins with a Zero, that will not be counted.
// So 0123456073 will give you 9 instead of 10.
// I felt this is nice to know as I struggled with it a little