There are a couple ways we can use JavaScript to get the decimal part of a number. One of the easiest ways is to convert the number into a string and make use of the JavaScript split() method.
Here is an example of how to do this:
var num = 123.456;
var numToString = num + ""; //Or num.toString()
var ArrayNum = numToString.split(".");
var decimalNumber = ArrayNum[1];
Our code above would have the variable decimalNumber be equal to 456 (the decimal part of our starting number). Let’s break down what is going on.
We start with our decimal number and then convert it to a string. This can be done easily by either String concatenation or using the toString() method.
We then use the split() method which will take our number and split it into an array of two strings, the first being the left part of the decimal, the second being the right part.
We finally get the decimal part of the number by selecting the second item in the array.
We can simplify this further and break the code down into one line.
var decimalNumber = (num + "").split(".")[1];
Where num is just the number we want to get the decimal part of.
We can finally put this code into a function so that all we have to do is enter a number into our function, and it will return the decimal part.
function getDecimalPart(num){
return (num + "").split(".")[1];
};
Using the indexOf() Method to Get the Decimal Part of a Number
Another simple way we can get the decimal part of a number is by using the indexOf() method. We simply convert our number to a string again, find the decimal point using the indexOf method, and then return the part after the decimal point using the substring() method.
Here is how to do this:
var num = 123.456;
var numToString = num + ""; //Or num.toString()
var indexPosition = numToString.indexOf(".");
var decimalPart = numToString.substring(indexPosition+1);
Or if you prefer as little code as possible:
var num = 123.456;
var decimalNumber = (num + "").substring((num + "").indexOf(".")+1);
Using JavaScript to Get the Decimal Part of Any Number
Below we will provide code to let the user input a number, and then use our getDecimalPart() function we created above to return the decimal part of the number if one exists. Here is our simple HTML setup:
Type a number that you want to get the decimal part of:
Below is the JavaScript code which gets the user input using the onclick event along with the value property and then uses our getDecimalPart() function to return the decimal part of the number.
We will finally update the results below using the textContent property.
Here is the JavaScript code:
function getDecimalPart() {
//Get the user inputted number
var userNum = document.getElementById("userVal").value;
//Get the decimal part
var decimalPart = (userNum + "").split(".")[1];
//Show the result to the user to the user
document.getElementById("results").textContent = decimalPart;
};
The final code and output for this example are below:
Code Output:
Type a number that you want to get the decimal part of:
Full Code:
Type a number that you want to get the decimal part of:
Hopefully this article has been useful in helping you understand how to use JavaScript to get the decimal part of a number.