/**
 * @param num The number to round
 * @param precision The number of decimal places to preserve
 */
function roundUp(num, precision) {
  precision = Math.pow(10, precision)
  return Math.ceil(num * precision) / precision
}

roundUp(192.168, 1) //=> 192.2
Answer from Andrew Marshall on Stack Overflow
🌐
pawelgrzybek
pawelgrzybek.com › rounding-and-truncating-numbers-in-javascript
Rounding and truncating numbers in JavaScript | pawelgrzybek.com
January 19, 2016 - Rounding up approximates a number towards positive infinity. Rounding down towards negative infinity. Truncating approximates without rounding. In other words, it “rounds” towards zero.
🌐
W3Schools
w3schools.com › jsref › jsref_ceil.asp
JavaScript Math ceil() Method
❮ Previous JavaScript Math Object ... let f = Math.ceil(-5.9); Try it Yourself » · The Math.ceil() method rounds a number rounded UP to the nearest integer....
Discussions

rounding - How to round up a number to a precision of tenths in JavaScript? - Stack Overflow
I want to use JavaScript to round up a number. Since the number is currency, I want it to round up like in these examples (2 decimal points): 192.168 => 192.20 192.11 => 192.20 192.21 =>... More on stackoverflow.com
🌐 stackoverflow.com
Why not always use Math.round instead of Math.floor?

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

More on reddit.com
🌐 r/javascript
13
1
February 22, 2017
rounding - How to round float numbers in javascript? - Stack Overflow
possible duplicate of round number in JavaScript to N decimal places -- please use the search before you ask a new question. ... @ShadowWizard is correct. However, .toFixed returns a string. Make sure to wrap the result in Number(). See accepted answer, and others. ... Sign up to request ... More on stackoverflow.com
🌐 stackoverflow.com
How to Round to the Nearest Integer in JavaScript
Have you ever seen Math.round(-2.5) return -2 instead of -3 and wondered why it behaves that way? Understanding the exact rules can prevent logic… More on reddit.com
🌐 r/webdev
1
0
July 4, 2025
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › round
Math.round() - JavaScript | MDN
If the fractional portion of the argument is greater than 0.5, the argument is rounded to the integer with the next higher absolute value. If it is less than 0.5, the argument is rounded to the integer with the lower absolute value.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › ceil
Math.ceil() - JavaScript | MDN
The Math.ceil() static method always rounds up and returns the smallest integer greater than or equal to a given number.
🌐
W3Schools
w3schools.com › jsref › jsref_round.asp
JavaScript Math round() Method
❮ Previous JavaScript Math Object ... Math.round(-2.50); let f = Math.round(-2.49); Try it Yourself » · The Math.round() method rounds a number to the nearest integer....
🌐
SitePoint
sitepoint.com › blog › javascript › a guide to rounding numbers in javascript
A Guide to Rounding Numbers in JavaScript — SitePoint
November 13, 2024 - This is the most straightforward option, and simply rounds any number with a decimal part to the nearest integer. It uses this rule: if a number is exactly halfway between two integers, it will be rounded up.
Find elsewhere
🌐
SheCodes
shecodes.io › athena › 27690-how-to-round-a-number-to-the-next-higher-one-in-javascript
[JavaScript] - How to round a number to the next higher one in JavaScript
Learn how to use the Math.ceil() method in JavaScript to round a number to the next higher integer. ... if I have a variable outside a function, do I need to redefine that variable inside the function? similarly, can use the same name for a ...
🌐
O'Reilly
oreilly.com › library › view › javascript-the-definitive › 0596101996 › re106.html
Math.ceil( ): round a number up — ECMAScript v1 - JavaScript: The Definitive Guide, 5th Edition [Book]
August 17, 2006 - Content preview from JavaScript: The Definitive Guide, 5th Edition · Math.ceil( ): round a number up — ECMAScript v1 · Math.ceil(x) x · Any numeric value or expression. The closest integer greater than or equal to x. Math.ceil( ) computes ...
Author   David Flanagan
Published   2006
Pages   1018
🌐
TutorialsPoint
tutorialspoint.com › how-to-round-up-a-number-in-javascript
How to round up a number in JavaScript?
November 7, 2022 - In the above syntax, first, we create variable ?x' which contains the value "A.B" after that we create another variable ?value' in which we assign integer x by applying the Math.ceil property to round up a number.
🌐
Educative
educative.io › answers › mathceil-mathfloor-and-mathround-in-javascript
Math.ceil, Math.floor, and Math.round in JavaScript
Math.round() rounds off a number depending on the fractional part of the number. So, if the fractional part is >=.5, it’ll return the smallest integer that is still greater than the passed value.
🌐
JavaScript in Plain English
javascript.plainenglish.io › math-ceil-math-round-and-math-floor-in-your-javascript-7142fde2c56d
Math.ceil, Math.round and Math.floor in Your JavaScript | by Piero Borrelli | JavaScript in Plain English
September 9, 2020 - Luckily for us, a language like JavaScript gives us tools like the Math object to deal with numbers and their operations. When it comes to rounding numbers, Math helps you with three main functions: Math.ceil, Math.floorand Math.round. This method rounds up the only passed in value to the nearest greater integer.
🌐
Peterlunch
peterlunch.com › snippets › javascript-round
How to round to decimal places in JavaScript?
June 12, 2021 - In the example above, we take the number 123.4567 and multiply it by 100 inside of the brackets. Then we divide that by 100 to give you a lovely number rounded to 2 decimal places all thanks to JavaScript and some basic math.
🌐
Reddit
reddit.com › r/javascript › why not always use math.round instead of math.floor?
r/javascript on Reddit: Why not always use Math.round instead of Math.floor?
February 22, 2017 -

When I read through the code of colleagues and public repos, I see Math.floor used like 20x more often than Math.round.

But why? Isn't Math.round more accurate than Math.floor? Shouldn't it be the other way around (using Math.round more often than Math.floor)?

Is Math.floor so much faster than Math.round or am I missing something?

Edit

I am aware that those two do different things. My point is that in my experience, Math.floor is much too often used, when Math.round would simply be more accurate.

Top answer
1 of 6
9

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

2 of 6
3

Math.floor - You have a rating system of stars, and you aren't breaking them up into half stars. You do a query to get all the votes and the math comes back to 4.7 stars. You would use Math.floor here so that you display 4 stars.

 

Math.ceil - You have a slider module that displays 3 slides at a time. This module contains 19 slides. 19/3 = 6.33. If you were to floor or round here you would end up with 6. But to make sure that all 19 slides are shown, you need 7 containers, so you use Math.ceil.

 

Math.round - Anytime you need the closest number without worrying about anything like the above scenarios.

🌐
Reintech
reintech.io › blog › using-the-math-round-method-for-javascript-contract-developers
Using the Math.round() Method | Reintech media
February 22, 2023 - It does this by first taking the number and calculating its remainder after division by 1. If the remainder is 0.5 or higher, it will round up; if it’s lower than 0.5, it will round down.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-round-method
JavaScript Math round() Method - GeeksforGeeks
September 16, 2024 - The JavaScript Math.round() method rounds a number to the nearest integer. It rounds up for decimal values of 0.5 and higher, and down otherwise.
🌐
CoreUI
coreui.io › blog › how-to-round-a-number-to-two-decimal-places-in-javascript
How to round a number to two decimal places in JavaScript · CoreUI
February 21, 2024 - Among these, rounding to two decimal places is particularly relevant across financial calculations, statistical reporting, and data presentation for its balance between precision and readability. This comprehensive guide will walk you through the intricacies of rounding numbers to two decimal places in JavaScript, ensuring you confidently apply this operation.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › floor
Math.floor() - JavaScript | MDN
/** * Adjusts a number to the specified digit. * * @param {"round" | "floor" | "ceil"} type The type of adjustment. * @param {number} value The number. * @param {number} exp The exponent (the 10 logarithm of the adjustment base). * @returns {number} The adjusted value.