Math.round to 2 Decimal Places in JavaScript

Today, we will discuss Math.round to 2 Decimal Places in JavaScript. If you want to learn new insights, read on!

In this article, you’ll discover how to round numbers to 2 decimal places in JavaScript using the Math.round() method, the toFixed() method, and a custom rounding function.

This tutorial is ideal for those dealing with financial calculations or situations where precision is critical.

How to round a number to 2 decimal places in JavaScript using Math.round?

Using Math.round() method you can round a number to 2 decimal places. Mainly, because this method rounds a number to the nearest integer.

Here’s the syntax of Math.round:

Math.round(x)

To round to two decimal places, you can multiply the number by 100 (or a power of 10 corresponding to the number of decimal places you want), round it to the nearest whole number, and then divide it by 100.

Here’s an example code:

// Define the number
var sampleNumber = 143.356;

// Round the number to 2 decimal places
var sampleRoundedNum = Math.round(sampleNumber * 100) / 100; ✅

// Display the result
console.log(sampleRoundedNum);

Output:

143.36

Additional solutions to round a number to 2 decimal places in JavaScript

Apart from using Math.round(), there are other methods that you can use to round a number to 2 decimal places in JavaScript

Use the toFixed() method

The toFixed() method is one of the best way to round a number to two decimal places in JavaScript.

Here’s the syntax of toFixed() method:

num.toFixed([decimal digits])

Here’s an example code:

var sampleNumber = 567.789;

var sampleRoundedNum = sampleNumber.toFixed(2);;✅

console.log(sampleRoundedNum);

Output:

567.79

Creating a custom function for rounding

This function uses exponential notation to first shift the decimal point in the number n places to the right (via “e+n”), rounds the result to the nearest integer, and then shifts the decimal point n places back to the left (via “e-n”).

Here’s the syntax:

function roundToTwo(num) { 
    return +(Math.round(num + "e+2")  + "e-2"); 
}

Here’s an example code:

function roundToTwo(num) { ✅
    return +(Math.round(num + "e+2") + "e-2");
}
var SampleNumber = 567.789;
var SampleroundedNumber = roundToTwo(SampleNumber);

console.log(SampleroundedNumber); 

Output:

567.79

📌Please note that JavaScript’s handling of floating-point arithmetic might lead to unexpected results in some cases due to precision errors.

If you’re dealing with financial calculations or other situations where precision is critical, consider using a library designed for precise decimal arithmetic.

Conclusion

In conclusion, we have explored how to round a number to 2 decimal places in JavaScript using several methods.

The Math.round() method and the toFixed() method are built-in JavaScript functions that make this task straightforward.

Also, we added creating a custom rounding function that is more flexible and has control over the rounding process.

We hope this article has provided you with enough information to understand the Math.round to 2 decimal places JavaScript.

If you want to explore more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment