• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Get the Decimal Part of a Number

Using JavaScript to Get the Decimal Part of a Number

June 24, 2022 Leave a Comment

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:

<p>Type a number that you want to get the decimal part of:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit" onclick="getDecimalPart()">
<div id="results"></div>

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:

<p>Type a number that you want to get the decimal part of:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit" onclick="getDecimalPart()">
<div id="results"></div>

<script>

function getDecimalPart() {
  var userNum = document.getElementById("userVal").value;
  var decimalPart = (userNum + "").split(".")[1];
  document.getElementById("results").textContent = decimalPart;   
};

</script>

Hopefully this article has been useful in helping you understand how to use JavaScript to get the decimal part of a number.

Other Articles You'll Also Like:

  • 1.  Remove Parentheses From String Using JavaScript
  • 2.  Using JavaScript to Get the Height of an Element
  • 3.  JavaScript Ceiling – Using Math.ceil() Method to Round Up to Ceiling
  • 4.  How to Call a JavaScript Function From HTML
  • 5.  Using the appendChild Method with JavaScript to Add Items to a List
  • 6.  How to Get the First Character of a String in JavaScript
  • 7.  Using JavaScript to Change the Font Size of a Paragraph
  • 8.  Using JavaScript to Get the Min Value in an Array
  • 9.  How to Split a Number into Digits in JavaScript
  • 10.  JavaScript onkeyup – How to Use onkeyup Event with JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy