
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- Count number of digits after decimal on dividing a number in C++
- Reversing the bits of a decimal number and returning new decimal number in JavaScript
- Count number of factors of a number - JavaScript
- Retrieving the decimal part only of a number in JavaScript
- How to get a decimal portion of a number with JavaScript?
- How to count a number of words in given string in JavaScript?
- How to convert a decimal number to roman using JavaScript?
- How can I remove the decimal part of JavaScript number?
- JavaScript Get English count number
- Count number of occurrences for each char in a string with JavaScript?
- How can I round a number to 1 decimal place in JavaScript?
- How to count the number of occurrences of a character in a string in JavaScript?
- How to count digits of given number? JavaScript
- Count the number of data types in an array - JavaScript
- How to count the number of elements in an array below/above a given number (JavaScript)
