var x = 1234567;
x.toString().length;
This process will only work for positive numbers that do not turn into exponential form with .toString().
var x = 1234567;
x.toString().length;
This process will only work for positive numbers that do not turn into exponential form with .toString().
Ok, so many answers, but this is a pure math one, just for the fun or for remembering that Math is Important:
var len = Math.ceil(Math.log(num + 1) / Math.LN10);
This actually gives the "length" of the number even if it's in exponential form. num is supposed to be a non negative integer here: if it's negative, take its absolute value and adjust the sign afterwards.
Update for ES2015
Now that Math.log10 is a thing, you can simply write
const len = Math.ceil(Math.log10(num + 1));
var x = 1234567;
x.toString().length;
This process will only work for positive numbers that do not turn into exponential form with .toString().
Ok, so many answers, but this is a pure math one, just for the fun or for remembering that Math is Important:
var len = Math.ceil(Math.log(num + 1) / Math.LN10);
This actually gives the "length" of the number even if it's in exponential form. num is supposed to be a non negative integer here: if it's negative, take its absolute value and adjust the sign afterwards.
Update for ES2015
Now that Math.log10 is a thing, you can simply write
const len = Math.ceil(Math.log10(num + 1));
length is a property, not a method. You can't call it, hence you don't need parenthesis ():
function getlength(number) {
return number.toString().length;
}
UPDATE: As discussed in the comments, the above example won't work for float numbers. To make it working we can either get rid of a period with String(number).replace('.', '').length, or count the digits with regular expression: String(number).match(/\d/g).length.
In terms of speed potentially the fastest way to get number of digits in the given number is to do it mathematically. For positive integers there is a wonderful algorithm with log10:
var length = Math.log(number) * Math.LOG10E + 1 | 0; // for positive integers
For all types of integers (including negatives) there is a brilliant optimised solution from @Mwr247, but be careful with using Math.log10, as it is not supported by many legacy browsers. So replacing Math.log10(x) with Math.log(x) * Math.LOG10E will solve the compatibility problem.
Creating fast mathematical solutions for decimal numbers won't be easy due to well known behaviour of floating point math, so cast-to-string approach will be more easy and fool proof. As mentioned by @streetlogics fast casting can be done with simple number to string concatenation, leading the replace solution to be transformed to:
var length = (number + '').replace('.', '').length; // for floats
Here's a mathematical answer (also works for negative numbers):
function numDigits(x) {
return Math.max(Math.floor(Math.log10(Math.abs(x))), 0) + 1;
}
And an optimized version of the above (more efficient bitwise operations): *
function numDigits(x) {
return (Math.log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1;
}
Essentially, we start by getting the absolute value of the input to allow negatives values to work correctly. Then we run through the log10 operation to give us what power of 10 the input is (if you were working in another base, you would use the logarithm for that base), which is the number of digits. Then we floor the output to only grab the integer part of that. Finally, we use the max function to fix decimal values (any fractional value between 0 and 1 just returns 1, instead of a negative number), and add 1 to the final output to get the count.
The above assumes (based on your example input) that you wish to count the number of digits in integers (so 12345 = 5, and thus 12345.678 = 5 as well). If you would like to count the total number of digits in the value (so 12345.678 = 8), then add this before the 'return' in either function above:
x = Number(String(x).replace(/[^0-9]/g, ''));
* Please note that bitwise operations in JavaScript only work with 32-bit values (max of 2,147,483,647). So don't go using the bitwise version if you expect numbers larger than that, or it simply won't work.
Well, I don't think providing length properties to number will be helpful. The point is the length of strings does not change by changing its representation.
for example you can have a string similar to this:
var b = "sometext";
and its length property will not change unless you actually change the string itself.
But this is not the case with numbers.
Same number can have multiple representations. E.g.:
var a = 23e-1;
and
var b = 2.3;
So its clear that same number can have multiple representations hence, if you have length property with numbers it will have to change with the representation of the number.
you must set variable toString() first, like this:
var num = 1024,
str = num.toString(),
len = str.length;
console.log(len);