Javascript Get the Length (Number of Digits) in a Number

Hello dev's, today I'll show you how to get the number of digits in a number. I'll show you 2 examples, to show how to get number of digits from a number both either it's an integer or float number. So, let's see how we can easily get number of digits.

Example 1

In the first example, we'll use an integer number to get the number of digits. Let's look at the below code.

const getLength = (number) => {
  return number.toString().length;
}

console.log(getLength(12345));

Here what we actually do is, first of all we'll create a method called getLength(), where we'll convert this number into string and then we'll calculate the length of that string. Thus it'll return 5. As we can see in the below picture.

Get the Length of an Integer Number

Example 2

In the first example, we'll use float/decimal number to get the number of digits. Let's look at the below code.

const getLengthOfFloat = (number) => {
  return parseInt(number).toString().length;
}

console.log(getLengthOfFloat(12345.12345));

Here what we actually do is, first of all we'll create a method called getLengthOfFloat(), where at first, we'll take the integer part of the number. Then we'll convert this number into string and finally we'll calculate the length of that string. Thus it'll return 5. As we can see in the below picture.

Get the Length of float/decimal Number

That's it for today. Hope you've enjoyed this tutorial. Catch me in the comment section if anything goes wrong. Thanks for reading.