Hướng dẫn javascript round to nearest

Round a Number Up to the Nearest 10 #

To round a number up to the nearest 10, call the Math.ceil() function, passing it the number divided by 10 as a parameter and then multiply the result by 10, e.g. Math.ceil(num / 10) * 10. The Math.ceil function rounds a number up to the next largest integer and returns the result.

Nội dung chính

  • Round a Number Up to the Nearest 10 #
  • Further Reading #
  • How do you round down to the nearest 10?
  • How do you round down in JavaScript?
  • How do you round up and down in JavaScript?
  • How do you round the number 3.12 to a closer integer in JavaScript?

Copied!

function roundUpNearest10(num) { return Math.ceil(num / 10) * 10; } console.log(roundUpNearest10(71)); // 👉️ 80 console.log(roundUpNearest10(79.9)); // 👉️ 80 console.log(roundUpNearest10(70.01)); // 👉️ 80 console.log(roundUpNearest10(-49)); // 👉️ -40 console.log(roundUpNearest10(-50)); // 👉️ -50

If you need to round up or down to the nearest 10, check out my other article - Round a Number (up or down) to the Nearest 10.

The Math.ceil function handles the heavy lifting for us.

If the passed in number has a fractional part, the Math.ceil function rounds the number up.

If an integer is passed, the function simply returns the number as is.

Here are some examples of using the Math.ceil function.

Copied!

console.log(Math.ceil(7.01)); // 👉️ 8 console.log(Math.ceil(71.00001)); // 👉️ 72 console.log(Math.ceil(70)); // 👉️ 70 console.log(Math.ceil(-23.99)); // 👉️ -20 console.log(Math.ceil(null)); // 👉️ 0

If the Math.ceil function is invoked with a null value, it returns 0.

Here is a step-by-step process of rounding a number to the nearest 10.

Copied!

console.log(21 / 10); // 👉️ 2.1 console.log(40 / 10); // 👉️ 4 console.log(Math.ceil(21 / 10)); // 👉️ 3 console.log(Math.ceil(40 / 10)); // 👉️ 4 console.log(Math.ceil(21 / 10) * 10); // 👉️ 30 console.log(Math.ceil(40 / 10) * 10); // 👉️ 40

There are 2 steps in the code:

  1. Divide the number by 10 and round the result up to the next largest integer.
  2. Multiply the result by 10 to get the number rounded up to the nearest 10.

Further Reading #

  • Round a Number Up to the Nearest 100 in JavaScript
  • Round a Number (up or down) to the Nearest 100 in JS

How do you round down to the nearest 10?

If the number you are rounding is followed by 5, 6, 7, 8, or 9, round the number up. Example: 38 rounded to the nearest ten is 40. If the number you are rounding is followed by 0, 1, 2, 3, or 4, round the number down. Example: 33 rounded to the nearest ten is 30.

How do you round down in JavaScript?

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

floor() - rounds down..

ceil() - rounds up..

How do you round up and down in JavaScript?

The Math. round() method rounds a number to the nearest integer. 2.49 will be rounded down (2), and 2.5 will be rounded up (3).

How do you round the number 3.12 to a closer integer in JavaScript?

You can use Math. ceil method to round up..

33 / 10 = 3.3..

3.3 rounded = 3..

3 × 10 = 30..