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 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment