Get number of digits with JavaScript
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 the 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 (so a 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.
Since this came up on a Google search for "javascript get number of digits", I wanted to throw it out there that there is a shorter alternative to this that relies on internal casting to be done for you:
var int_number = 254;
var int_length = (''+int_number).length;
var dec_number = 2.12;
var dec_length = (''+dec_number).length;
console.log(int_length, dec_length);
Yields
3 4
it would be simple to get the length as
`${NUM}`.length
where NUM is the number to get the length for