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

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

In the code above, **num** will be the number that we want to round to 4 decimal places.

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

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

Which would result in the following output:

`2.3547`

One thing to note is that the above code will round any number to a maximum of 4 decimal places. If a number does not have at least 4 decimals, it will **not** return the number with 4 decimal points.

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

## Round a Number to Exactly 4 Decimals Places

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

```
<p>Type a number you want to round to 4 decimal places:</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 takes the user input using the onkeyup event or the **onclick event** along with the value property and uses the Math.round() method on that user input and updates the results below using the textContent property.

We talked about how we always want to return a number with 4 decimal places. So if the user types in the number 3, we want to return 3.0000.

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 4 decimal places, so use the code from the example above
roundedNumber = Math.round(Number(userNum) * 10000) / 10000;
//Convert Number to string to check decimal places
roundedNumber = roundedNumber + "";
//Check if number had a decimal point
if(roundedNumber.indexOf('.') != -1){
var decimalPlaces = roundedNumber.substring(roundedNumber.indexOf('.'));
//Check if decimal has one place
if( decimalPlaces.length == 2 ){
//Number only has one decimal place, so add a 000
roundedNumber = roundedNumber + '000';
} else if( decimalPlaces.length == 3 ){
//Number only has two decimal places, so add a 00
roundedNumber = roundedNumber + '00';
} else if( decimalPlaces.length == 4 ){
//Number only has three decimal places, so add a 0
roundedNumber = roundedNumber + '0';
}
} else {
//Number has 0 decimal places, so add three
roundedNumber = roundedNumber + '.0000';
}
//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 4 decimal places:

Full Code:

```
<p>Type a number you want to round to 4 decimal places:</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) * 10000) / 10000;
roundedNumber = roundedNumber + "";
if(roundedNumber.indexOf('.') != -1){
var decimalPlaces = roundedNumber.substring(roundedNumber.indexOf('.'));
if( decimalPlaces.length == 2 ){
roundedNumber = roundedNumber + '000';
} else if( decimalPlaces.length == 3 ){
roundedNumber = roundedNumber + '00';
} else if( decimalPlaces.length == 4 ){
roundedNumber = roundedNumber + '0';
}
} else {
roundedNumber = roundedNumber + '.0000';
}
document.getElementById("results").textContent = roundedNumber + "";
}
};
</script>
```

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

## Leave a Reply