• 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 Round to 1 Decimal

Using JavaScript to Round to 1 Decimal

April 17, 2022 Leave a Comment

In JavaScript, to round a number to 1 decimal place we can use the Math.round() method in the following JavaScript code:

var roundedNumber = Math.round(num * 10) / 10;

In the code above, num will be the number that we want to round to 1 decimal place.

So if we want to round the number 2.354678 to 1 decimals place, we would use the following JavaScript:

var roundedNumber = Math.round(2.354678 * 10) / 10;

Which would result in the following output:

2.4

One thing to note is that the above code will round any number to a maximum of 1 decimal place. If a number does not have at least 1 point, it will not return the number with 1 decimal point.

If we want the number to always have 1 decimal point, then we need to add a little more code. Let’s do this in the example below.

Round a Number to Exactly 1 Decimals Point

Below we will provide code to let the user input a number, and then use the code above to round the number to 1 decimal point. Here is our simple HTML setup:

<p>Type a number you want to round to 1 decimal place:</p>
<input id="userVal" type="text" value="" onkeyup="roundNum(event)">
<input id="submitNum" type="submit" value="Submit" onclick="roundNum('click')">
<div id="results"></div>

Below is the JavaScript code which take the user input using the onkeyup event or the onclick event along with the value property and use the Math.round() method on that user input and update the results below using the textContent property.

We talked about how we always want to return a number with 1 decimal place. So if the user types in the number 2, we want to return 2.0.

To do this, we just need to add a little more code to check the length of the decimal, using the indexOf() method.

function roundNum(e) {
  var roundedNumber;
  //If we clicked or pressed enter, run the following code 
  if( e == 'click' || e.keyCode == 13 ){
    var userNum = document.getElementById("userVal").value;
    
    //Round number to 1 decimal place, so use the code from the example above
    roundedNumber = Math.round(Number(userNum) * 10) / 10;
    
    //Convert Number to string to check decimal places
    roundedNumber = roundedNumber + "";

    //Check if number does not have a decimal point
    if(roundedNumber.indexOf('.') == -1){
      //Number has 0 decimal places, so add one
        roundedNumber = roundedNumber + '.0';
    }
    //Show the rounded number to the user
    document.getElementById("results").textContent = roundedNumber + "";   
  }
};

The final code and output for this example are below:

Code Output:

Type a number you want to round to 1 decimal place:


Full Code:

<p>Type a number you want to round to 1 decimal place:</p>
<input id="userVal" type="text" value="" onkeyup="roundNum(event)">
<input id="submitNum" type="submit" value="Submit" onclick="roundNum('click')">
<div id="results"></div>

<script>

function roundNum(e) {
  var roundedNumber;
  if( e == 'click' || e.keyCode == 13 ){
    var userNum = document.getElementById("userVal").value;
    roundedNumber = Math.round(Number(userNum) * 10) / 10;
    roundedNumber = roundedNumber + "";
    if(roundedNumber.indexOf('.') == -1){
        roundedNumber = roundedNumber + '.0';
    }
    document.getElementById("results").textContent = roundedNumber + "";   
  }
};

</script>

Hopefully this article has been useful in helping you understand how to use JavaScript to round to 1 decimal.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Increment a Variable by 1
  • 2.  Set Checkbox as Checked in JavaScript
  • 3.  Add Minutes to a Date Using JavaScript
  • 4.  Using JavaScript to Convert a String to Uppercase
  • 5.  Add Years to a Date Using JavaScript
  • 6.  Using JavaScript to Get the Domain From URL
  • 7.  Using JavaScript to Get the URL Path
  • 8.  Using JavaScript to Remove Backslash From a String
  • 9.  Using JavaScript to Check a Radio Button
  • 10.  Using JavaScript to Replace Space With Dash

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