Explain Codes LogoExplain Codes Logo

How can I round down a number in Javascript?

javascript
functions
performance
floating-point
Anton ShumikhinbyAnton Shumikhin·Feb 10, 2025
TLDR

Use Math.floor() in JavaScript for rounding down, like this:

let num = Math.floor(5.95); // Sparta kicks decimals out to return 5

Overview of rounding functions

In JavaScript, to banish the pesky fractions and tame numbers to their nearest integer counterparts, Math.floor() is the most straight-shooting superhero in your arsenal.

However, the plot thickens when our numbers turn rogue with their negative sign. While Math.floor() always rounds in the direction of minus infinity, Math.trunc() deflects the negatives and rounds in the direction of zero.

let numNegative = Math.trunc(-5.95); // Discards decimals, returns -5 against -6 by Math.floor()

When performance is the need of the hour, rounding down using Bitwise OR | operator can be faster, but remember it delivers only for non-negative numbers.

let bitwiseRoundDown = 5.95 | 0; // Exchanges clarity for speed, returns 5

Precision control: rounding with decimal places

For rounding down to a fixed number of decimal places, combine Math.floor() with Math.pow():

function roundDown(value, decimals) { return Math.floor(value * Math.pow(10, decimals)) / Math.pow(10, decimals); } let preciseRoundDown = roundDown(5.987, 2); // Iced down to two decimals, returns 5.98

Though it may seem like precise control, always verify the results when dealing with values destined for scientific or financial calculations due to potential discrepancies in floating-point arithmetic.

Edge cases and compatibility

Math.floor() proves itself reliable across different platforms, but let's not miss out on the slight quirks it brings in handling certain special scenarios:

  • Negative zero: Math.floor(-0) will return -0, causing hiccups in string formatting and calculations.
  • Infinities: JavaScript plays fair with infinities, with -Infinity being less than all numbers and Infinity greater than all.
console.log(Math.floor(Infinity)); // Says hi to Infinity console.log(Math.floor(-Infinity)); // Bows low to -Infinity

Pro tip: Keep the official documentation bookmarked for complete and updated information!

Working with floating point numbers

Touching the world of floating point numbers, you might stumble into a twilight zone due to the way JavaScript handles decimals. Unreliable rounding, repeating decimals, and numbers very close to the next integer are some nooks and crannies that you might get lost in. But fear not, as we prepare for some common hurdles and their workarounds:

Ignoring fraction part

To help you round down by ignoring the fraction:

let ignoreFraction = 5.95 - (5.95 % 1); // Shows fraction the exit door, returns 5

This brutally cuts off the fractional part by calculating the remainder when divided by 1, thus effectively rounding down.

Helping hand from libraries

For scenarios demanding high precision, you can lean on libraries like BigNumber.js or decimal.js, designed as a knight in shining armor to handle JavaScript's number type limitations.

Compatibility with Math.ceil()

Taking cross-browser compatibility by the horns, we find that Math.floor() might not always behave for older browsers. In such situations, we can call upon Math.ceil() with negation to do our bidding:

let badBrowserRoundDown = Math.ceil(-5.95) * -1; // Rounding up the down way, returns -5