Chances are, growing up, you ran into at least one person who prided him/herself on being able to recite the value of Pi to the bazillionth digit:
3.14159265358979323846264338327950288419716939937510 58209749445923078164062862089986280348253421170679 82148086513282306647093844609550582231725359408128 48111745028410270193852110555964462294895493038196...
Maybe you knew this kid:
Anyway, for most real-world situations, you don't need to go into a whole lot of detail. Something less precise for Pi...such as 3.14159 will do just fine.
This issue of precision will come up quite frequently as you work with numbers in JavaScript. This is where rounding comes in. Rounding a number is all about taking something that is extra precise and making it less precise...for good reason. In this tutorial, you will learn a bunch of tricks on how to do that.
Onwards!
Why Round Numbers?
You may be wondering why anybody would want to go from having values that are more precise to having values that are less precise. In this section, we are going to look at some examples.
Displaying on the Pixel
Whenever we draw on a canvas or position DOM elements, we choose how coordinates map to pixels. Some sharp edges benefit from alignment, but fractional coordinates are not automatically wrong. Text rendering, device scale, and transforms all affect what we see. Here is text at a few pixel scales:

Rounding a coordinate gives a whole value in that coordinate system. A whole CSS pixel is not necessarily a physical pixel boundary, and a canvas stroke's width also affects its alignment. Check the actual rendering. If we round for display, we can keep the original fractional position for later calculations.
Improving Performance
Whole numbers are not a universal shortcut to faster JavaScript. The result depends on the runtime and the work we are doing, and rounding has a cost of its own. Measure before changing the data. In an animation, rounding the stored position after every tiny movement can discard the progress we meant to accumulate.
Dealing with JavaScript's Quirky Number Handling
JavaScript's Number type uses binary floating point. Some decimal fractions cannot be represented exactly, so their approximations affect calculations. BigInt is separate and represents integers, not fractional amounts. Here is an example using Number values:
var blah = 0.2 * 0.4;
What do you think the value of blah actually is? If you guessed .08, you would be wrong. Here is what actually shows up:
There is a reason why the 2 awkwardly shows up at the end of the result. Our decimal fractions were represented using binary approximations. Rounding or formatting can give us a displayed value of .08, but it does not turn later Number calculations into exact decimal arithmetic. If we need exact cents, one option is to parse integer cents directly from validated decimal text and keep every amount and intermediate total within the safe-integer range.
Rounding the Number
Whatever your reason for rounding numbers may be, JavaScript has several ways of allowing you to round numbers. Let us look at the most common ones.
Rounding to the Nearest Integer
The most common function for rounding your value is the prom king himself, Math.round:
Math.round(1.532); // 2 Math.round(1.235); // 1 Math.round(27.94); // 28 Math.round(0.0005); // 0
This function rounds to the nearest integer. When a finite value is exactly halfway between two integers, Math.round chooses the one toward positive infinity. That tie rule matters for negative values too, so it may differ from the rule we learned in school.
Its equally popular variants are Math.floor and Math.ceil. For a finite input, floor returns the greatest integer less than or equal to that value. Ceil returns the smallest integer greater than or equal to it. Notice the equal to part: an integer already satisfies either rule.
Math.ceil(1.5); // 2 Math.floor(1.5); // 1 Math.floor(1.235); // 1 Math.ceil(0.0005); // 1
All of this should look pretty straightforward. Just to drive the point home about the next highest and lowest integers, let's look what happens when we are dealing with negative values:
Math.ceil(-45) // -45 Math.floor(-45) // -45 Math.ceil(-.5) // -0 Math.floor(-.5) // -1 Math.round(-45) // -45 Math.round(-.5) // -0 Math.round(-1.24) // -1
For negative fractions, moving toward a greater integer means moving toward zero; moving toward a smaller integer means moving away from zero. Already integral inputs stay put. Our number line makes those directions visible:

That is why ceiling a value of -.5 reaches zero rather than -1. JavaScript preserves a negative zero in this case, so the exact result is -0. On the number line, ceil moves right and floor moves left until the required integer is reached. Neither needs to move when the input is already an integer.
Our prom king Math.round also resolves a halfway tie toward the right. Rounding -.5 gives -0, and rounding -1.5 gives -1. It does not round negative halfway values away from zero. If a refund or measurement policy requires that different rule, positive-only examples will not catch the mismatch.
Rounding to Arbitrary Levels of Precision
In the previous section, you learned how you can round to the nearest integer. There will be times when you want to round to a particular decimal position for extra preciseness. Let's say we want to round our value of Pi to just 2 digits past the decimal point. In other words, we want 3.14159 to be rounded off to 3.14. Using the three amigos (Math.round, Math.floor, and Math.ceil) without any modification will cause our value to either be 3 or 4. That's not what we want.
To get what we want, there is a simple approach we can take made up of just three steps:
- To get X digits of precision, multiply our number by 10 to the power of X.
- Round the result using Math.round()
- Divide the new result by 10 to the power of X again.
To make this more concrete, let's see we want to round 3.14159 to two digits of precision. To get two decimal positions of precision, we can multiply our number by 10 to the power of two (aka 100) and round that value:
Math.round(3.14159 * 100);
Next, divide the result of this rounding operation by 10 to the power of two again to get the answer you are looking for:
Math.round(3.14159 * 100) / 100; // result is 3.14
To generalize this a bit, here is a basic formula:

The d stands for the number of decimal places, and number is the value we want to round. Scaling lets an integer-rounding operation act at that decimal position. The calculation still uses binary floating point, so this formula is an approximation technique rather than a guarantee of exact decimal rounding.
Using this model, here is how we can round 3.14159 to the tenths, hundredths, thousandths, and ten thousandths positions:
Math.round(3.14159 * 10) / 10; // 3.1 Math.round(3.14159 * 100) / 100; // 3.14 Math.round(3.14159 * 1000) / 1000; // 3.142 Math.round(3.14159 * 10000) / 10000; // 3.1416
We can substitute Math.floor or Math.ceil when the required direction differs. We still need to account for floating-point limits. For example, Math.round(1.005 * 100) / 100 produces 1 rather than 1.01 because the scaled value falls below the expected halfway point. Test the actual boundary cases and choose a suitable representation when exact decimal rounding is required.
Conclusion
Normally, I would have something witty to say here. Actually, who am I kidding? My conclusions are usually pretty awful. In fact, nobody even reads this section. I could write whatever I want and nobody would notice. Besides, what is there to conclude about rounding numbers?
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

