
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 in the above snippet there are 8 digits in the floating point number after the decimal point.
To achieve the above task, we used three methods. Namely isInteger(), toString() and, split() method. Let's see them one by one -
Number.isInteger()method
The Number.isInteger() method in JavaScript checks the passed value is an integer or not. It will return true if passed value is an integer and false if it is not.
Syntax
Following is the syntax of Number.isInteger() method in JavaScript -
Number.isInteger(val)
Where the val parameter is the value to be tested. And the return type of this method is a Boolean value.
The toString()method
The toString() method in JavaScript will return the number as a string. The return type will be a string.
Syntax
Following is the syntax of toString() method in JavaScript -
number.toString()
The split()method
The split() method in JavaScript will split a string into an array of substrings. It will return a new array and this method will not change or modify the original string.
Syntax
Following is the syntax of split() method in JavaScript -
string.split(separator, limit)
Where,
The separator parameter is a pattern describing where each split should happen. A string or a regular expression can be used for splitting.
The limit parameter is an integer that limits the number of splits.
Now, we'll be using all the three above methods in the example below to get the count of the digits after the decimal point.
Example
In the example below, we have declared a floating point number. The Number.isInteger() method will check whether the passed number is an integer or not.
As it a floating point number, the toString() method will convert the number into string and the split() method will split the string into array of substrings(digits before decimal point and digits after decimal point) and the length property will get the length of the digits after decimal point.
<!DOCTYPE html> <html> <head> <title>Decimal count of a number </title> <button onClick = "func()">Click!</button> <p id = "para1"></p> </head> <body> <script> const numb = 45.36346323 document.getElementById("para1").innerHTML = numb; function func(){ function count(numb) { if (Number.isInteger(numb)) { return 0; } else { return numb.toString().split('.')[1].length; } } document.getElementById("para1").innerHTML = "Decimal count: " + count(numb); } </script> </body> </html>
As we can see in the output, we printed the count of the digits after the decimal point.
Using includes()method
This method is used to verify whether the current string contains a given substring. This method returns true if the search value contains a specified string and false if not. This method is case sensitive.
Syntax
Following is the syntax of includes() method in JavaScript -
string.includes(searchValue, start)
Where,
The searchValue is the value to search.
The start is the position to begin the search of searchValue.
We've also used the above method in the process of counting the digits after the decimal point.
Example
In this following example below, we've converted the number into string and checked whether decimal point (.) is included in the string or not. Then we've split the string into two subarrays and length property counted the length of subarray which contains digits after the decimal point.
<!DOCTYPE html> <html> <head> <title>Decimal count of a number </title> <button Onclick = "btn()"> Click to get the count </button> <p id = "para1"></p> <p id = "para2"></p> </head> <body> <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>
As we can see in the output, we printed the count of the digits after the decimal point.