In JavaScript, you have to handle numbers and strings for different purposes, and it may be needed to divide the number into its integer and decimal components. For instance, the number 11.5 would be divided into two numbers, 11 and 0.5. The digit after the decimal point (.) is a decimal part of the number, such as 5 being the decimal part of the number 11.5.
This blog will provide the methods to get the decimal part of a number.
How to Get Decimal Part of Number in JavaScript?
You can use some predefined methods of JavaScript to get the decimal part of a number. These methods are mentioned below:
-
- indexOf() method
- split() method
Let’s see the working of both of them one by one!
Method 1: Get Decimal Part of Number Using indexOf() Method
The “indexOf()” method can be utilized to get the decimal part from a number. In this approach, firstly, you have to convert the number to a string and get the index of the decimal point (.) with the help of the “indexOf()” method. Then, extract the part after the decimal point as a substring utilizing the “substring()” method.
Syntax
Follow the below-given syntax for getting the decimal part from number using indexOf() method:
Example
In this example, first we will create a variable that stores a decimal number “15.115”:
Here, we will first convert the number to the string, then get the index of the decimal point, and store it in a new variable:
After getting the index of the decimal point, we will again convert the number to the string and get the part of the number after the decimal point using the substring() method and lastly store it in a new variable named “decimalPart”:
Finally, we will print the decimal part of the number that is stored as a value of the variable “decimalPart” on the console:
As you can see, the output displayed the decimal part of the number:
Let’s head toward the second method!
Method 2: Get Decimal Part of Number Using split() Method
The “split()” method in JavaScript is another way to determine a decimal part of a number. It is the most frequently and simply used method for getting the part of a number. It splits the string into an array of substrings and gives the new array as output.
Syntax
Use the following method for the “split()” method:
It takes two parameters, the separator and the limit. The second parameter is optional, while the first parameter, “separator”, is used to split the string. The “limit” specifies how many splits there can be. Note that the Items that exceed the split limit won’t be displayed in the array. The split items are returned in the new array.
Example
Here, we will use the same number created in the previous example. Then, we will first convert the number into the string using the “toString()” method and split the number by passing the decimal point (.) as a separator. Then, convert the array element at index 1 for fetching the decimal part of the number:
Finally, print the decimal part of the number on the console:
Output
We have gathered all the methods for getting the decimal part of a number.
Conclusion
For getting the number’s decimal part, you can use the “split()” method and the “indexOf()” method. In the indexOf() method, you will first convert your number to a string and get the index of the decimal point (.) and get the part after the decimal point as a substring using the substring() method. In the split() method, first split the string based on the separator and get the number after the separator. In this blog post, we have provided the methods for getting the decimal part of a number with detailed examples.