Decimal count of a Number in JavaScript

Given there is a number with digits after decimal point and the task is to find the count of digits after the decimal point.

Input Output Scenario

Let's look into the input output scenario, where there is a floating point number having some digits after decimal point.

Input = 45.36346323
Output = 8

As we can see in the above snippet there are 8 digits in the floating point number after the decimal point.

To achieve this task, we'll explore three different methods using isInteger(), toString(), and split() methods.

Method 1: Using Number.isInteger() and split()

The Number.isInteger() method checks if the passed value is an integer. It returns true if the value is an integer and false if it's not.

Syntax

Number.isInteger(val)

Example

We'll convert the number to a string, check if it contains a decimal point, and then count the digits after it:

<!DOCTYPE html>
<html>
<head>
    <title>Decimal count of a number</title>
</head>
<body>
    <button onclick="func()">Click to count decimals!</button>
    <p id="para1"></p>
    
    <script>
        const numb = 45.36346323;
        document.getElementById("para1").innerHTML = "Number: " + numb;
        
        function func() {
            function count(numb) {
                if (Number.isInteger(numb)) {
                    return 0;
                } else {
                    return numb.toString().split('.')[1].length;
                }
            }
            document.getElementById("para1").innerHTML = "Number: " + numb + "<br>Decimal count: " + count(numb);
        }
    </script>
</body>
</html>

Method 2: Using includes() Method

The includes() method verifies whether the current string contains a given substring. It returns true if the search value is found and false if not. This method is case sensitive.

Syntax

string.includes(searchValue, start)

Parameters:

  • searchValue - The value to search for

  • start - The position to begin the search (optional)

Example

Convert the number to a string and check if it includes a decimal point, then count the digits after it:

<!DOCTYPE html>
<html>
<head>
    <title>Decimal count using includes()</title>
</head>
<body>
    <button onclick="btn()">Click to get the count</button>
    <p id="para1"></p>
    <p id="para2"></p>
    
    <script>
        function btn() {
            function count(num) {
                const converted = num.toString();
                if (converted.includes('.')) {
                    return converted.split('.')[1].length;
                }
                return 0;
            }
            
            document.getElementById("para1").innerHTML = "Decimal count of 56.234235 is: " + count(56.234235);
            document.getElementById("para2").innerHTML = "Decimal count of 32856456 is: " + count(32856456);
        }
    </script>
</body>
</html>

Method 3: Using Regular Expression

We can also use regular expressions to extract and count decimal places:

<!DOCTYPE html>
<html>
<head>
    <title>Decimal count using RegEx</title>
</head>
<body>
    <button onclick="regexMethod()">Count with RegEx</button>
    <p id="result"></p>
    
    <script>
        function regexMethod() {
            function countDecimals(num) {
                const str = num.toString();
                const match = str.match(/\.(\d+)/);
                return match ? match[1].length : 0;
            }
            
            const testNumbers = [45.36346323, 123, 7.5, 0.123456];
            let output = "Decimal counts:<br>";
            
            testNumbers.forEach(num => {
                output += `${num}: ${countDecimals(num)} decimal places<br>`;
            });
            
            document.getElementById("result").innerHTML = output;
        }
    </script>
</body>
</html>

Comparison

Method Readability Performance Complexity
Number.isInteger() + split() High Good Simple
includes() + split() High Good Simple
Regular Expression Medium Better Advanced

Conclusion

All three methods effectively count decimal places in JavaScript numbers. The split() approach is most readable for beginners, while regular expressions offer better performance for large-scale applications.

Updated on: 2026-03-15T23:19:00+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements