🌐
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.
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › round
Math.round() - JavaScript | MDN
console.log(Math.round(0.9)); // Expected output: 1 console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05)); // Expected output: 6 6 5 console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95)); // Expected output: -5 -5 -6
🌐
Ihechikara
ihechikara.com › posts › how-to-round-a-number-in-js
How To Round Numbers in JavaScript – Explained With Code Examples
In the example above, we created a variable called num with a value of 2.5. Using the Math.round() method, we rounded the number to the closest integer, which is 3. If the digit after the decimal point is greater than or equal to 5, then the ...
🌐
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....
🌐
pawelgrzybek
pawelgrzybek.com › rounding-and-truncating-numbers-in-javascript
Rounding and truncating numbers in JavaScript | pawelgrzybek.com
Rounding is straight forward. We can round to the nearest integer, round down or round up. JavaScript uses three methods to achieve this: Math.round() - rounds to the nearest integer (if the fraction is 0.5 or greater - rounds up)
🌐
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.
🌐
Educative
educative.io › answers › mathceil-mathfloor-and-mathround-in-javascript
Math.ceil, Math.floor, and Math.round in JavaScript
... So, if we pass the value 1.4 in Math.floor, we’ll get 1 in return. If we pass 1.6, we’ll also get 1. ... Math.round() rounds off a number depending on the fractional part of the number.
🌐
TechOnTheNet
techonthenet.com › js › math_round.php
JavaScript: Math round() function
In JavaScript, the syntax for the round() function is: ... The number that will be rounded. The round() function returns a number rounded to the nearest integer. In other words, the number is rounded to 0 decimal places. If the number is a negative value, the round() function rounds up towards ...
Find elsewhere
🌐
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....
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › ceil
JavaScript Math ceil() - Round Up Value | Vultr Docs
November 29, 2024 - The Math.ceil() function in JavaScript is a versatile tool that helps round any given number up to the nearest integer. Its straightforward implementation and predictable behavior with floating-point numbers, negative values, zeros, integers, ...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › round
JavaScript Math round() - Round Number to Integer | Vultr Docs
December 2, 2024 - This script takes 47.8, divides it by 10 to shift the decimal place, rounds it, and then multiplies it back by 10. The result is 50. Edge cases include numbers exactly halfway between two integers. Evaluate how Math.round() resolves these scenarios.
🌐
Math.js
mathjs.org › docs › reference › functions › round.html
math.js | an extensive math library for JavaScript and Node.js
Round a value towards the nearest rounded value. For matrices, the function is evaluated element wise. math.round(x) math.round(x, n) math.round(unit, valuelessUnit) math.round(unit, n, valuelessUnit)
Top answer
1 of 12
1032

NOTE - See Edit 4 if 3 digit precision is important

var discount = (price / listprice).toFixed(2);

toFixed will round up or down for you depending on the values beyond 2 decimals.

Example: http://jsfiddle.net/calder12/tv9HY/

Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Edit - As mentioned by others this converts the result to a string. To avoid this:

var discount = +((price / listprice).toFixed(2));

Edit 2- As also mentioned in the comments this function fails in some precision, in the case of 1.005 for example it will return 1.00 instead of 1.01. If accuracy to this degree is important I've found this answer: https://stackoverflow.com/a/32605063/1726511 Which seems to work well with all the tests I've tried.

There is one minor modification required though, the function in the answer linked above returns whole numbers when it rounds to one, so for example 99.004 will return 99 instead of 99.00 which isn't ideal for displaying prices.

Edit 3 - Seems having the toFixed on the actual return was STILL screwing up some numbers, this final edit appears to work. Geez so many reworks!

var discount = roundTo((price / listprice), 2);

function roundTo(n, digits) {
  if (digits === undefined) {
    digits = 0;
  }

  var multiplicator = Math.pow(10, digits);
  n = parseFloat((n * multiplicator).toFixed(11));
  var test =(Math.round(n) / multiplicator);
  return +(test.toFixed(digits));
}

See Fiddle example here: https://jsfiddle.net/calder12/3Lbhfy5s/

Edit 4 - You guys are killing me. Edit 3 fails on negative numbers, without digging into why it's just easier to deal with turning a negative number positive before doing the rounding, then turning it back before returning the result.

function roundTo(n, digits) {
    var negative = false;
    if (digits === undefined) {
        digits = 0;
    }
    if (n < 0) {
        negative = true;
        n = n * -1;
    }
    var multiplicator = Math.pow(10, digits);
    n = parseFloat((n * multiplicator).toFixed(11));
    n = (Math.round(n) / multiplicator).toFixed(digits);
    if (negative) {
        n = (n * -1).toFixed(digits);
    }
    return n;
}

Fiddle: https://jsfiddle.net/3Lbhfy5s/79/

2 of 12
172

If you use a unary plus to convert a string to a number as documented on MDN.

For example:+discount.toFixed(2)

🌐
CoreUI
coreui.io › answers › how-to-round-a-number-in-javascript
How to round a number in JavaScript · CoreUI
September 26, 2025 - The Math.round() function rounds a number to the nearest integer using standard mathematical rounding (0.5 rounds up). In the first example, Math.round(3.7456) returns 4 because 3.7456 is closer to 4 than to 3. For decimal precision, multiply ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › floor
Math.floor() - JavaScript | MDN
It does so by multiplying the number by a power of 10, then rounding the result to the nearest integer, then dividing by the power of 10.
🌐
CodeParrot
codeparrot.ai › blogs › javascript-round-to-2-decimal-places-a-complete-guide
JavaScript Round to 2 Decimal Places: A Complete Guide
JavaScript's floating-point arithmetic can cause unexpected results, especially in JavaScript Round to 2 Decimal Places. Adding Number.EPSILON—the smallest possible value that can be added to 1 to yield a result greater than 1—helps mitigate these errors. const roundWithEpsilon = (num) => Math.round((num + Number.EPSILON) * 100) / 100; console.log(roundWithEpsilon(1.005)); // Output: 1.01 console.log(roundWithEpsilon(1.255)); // Output: 1.26
🌐
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 - NameMath.ceil( ): round a number up — ECMAScript v1SynopsisMath.ceil(x)Arguments x Any numeric value or expression.ReturnsThe closest integer greater than or equal to... - Selection from JavaScript: The Definitive Guide, 5th Edition [Book]
Author   David Flanagan
Published   2006
Pages   1018