• 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
  • VBA
  • About
You are here: Home / JavaScript / How to Remove Decimals of a Number in JavaScript

How to Remove Decimals of a Number in JavaScript

June 24, 2022 Leave a Comment

The easiest way to remove the decimals of a number in JavaScript is by using the Math.trunc() method.

var noDecimalNumber = Math.trunc(9.31);

We can wrap this code in a function to simplify the whole process. You simply enter in a number, and the function will return the number with no decimal places.

function noDecimalNumber(num){
  return Math.trunc(num);
};

Here are some more examples using our function:

var num = noDecimalNumber(1.123);
var num1 = noDecimalNumber(.6789);
var num2 = noDecimalNumber(1020.12);
var num3 = noDecimalNumber(-.54);
var num4 = noDecimalNumber(-50.98283);

Which would result in the following:

1
0
1020
0
-50

Remove Decimals of Any Number in JavaScript

Below we will provide code to let the user input a number, and then use our noDecimalNumber() function we created above to return the number without decimals. Here is our simple HTML setup:

<p>Type a number:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit" onclick="noDecimalNumber()">
<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 noDecimalNumber() function to return the number with no decimals.

We will finally update the results below using the textContent property.

Here is the JavaScript code:

function noDecimalNumber() {
  //Get the user inputted number
  var userNum = document.getElementById("userVal").value;

  //Remove the decimal if one exists 
  var num = Math.trunc(userNum);

  //Show the result to the user to the user
  document.getElementById("results").textContent = num;   
};

The final code and output for this example are below:

Code Output:

Type a number:


Full Code:

<p>Type a number:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit" onclick="noDecimalNumber()">
<div id="results"></div>

<script>

function noDecimalNumber() {
  var userNum = document.getElementById("userVal").value;
  var num = Math.trunc(userNum);
  document.getElementById("results").textContent = num;   
};

</script>

Hopefully this article has been useful in helping you understand how to remove decimals from a number in JavaScript.

Other Articles You'll Also Like:

  • 1.  How to Iterate through an Array Using JavaScript
  • 2.  JavaScript Trig – How to Use Trigonometric Functions in Javascript
  • 3.  Insert Character Into String In JavaScript
  • 4.  Check if a String Contains Uppercase Letters in JavaScript
  • 5.  How to Uncheck All Checkboxes in JavaScript
  • 6.  Using JavaScript to Check if Variable Exists
  • 7.  Add Minutes to a Date Using JavaScript
  • 8.  JavaScript atan2 – Find Arctangent of the Quotient of Two Numbers
  • 9.  Using JavaScript to Replace a Space with an Underscore
  • 10.  Set the Height of a Div Using 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy