Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round a float up to the next integer in javascript

I need to round floating point numbers up to the nearest integer, even if the number after the point is less than 0.5.

For example,

  • 4.3 should be 5 (not 4)
  • 4.8 should be 5

How can I do this in JavaScript?

like image 766
Heba Gomaah Avatar asked Jun 25 '12 10:06

Heba Gomaah


People also ask

How do you round up to the next integer?

Rounding to the Nearest Integer If the digit in the tenths place is less than 5, then round down, which means the units digit remains the same; if the digit in the tenths place is 5 or greater, then round up, which means you should increase the unit digit by one.

How do you round off to the next number 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 7.25 to the nearest integer in JavaScript?

The Math. round() function in JavaScript is used to round the number passed as parameter to its nearest integer. Parameters : The number to be rounded to its nearest integer.

Which math function in JavaScript rounds a number up to the next integer?

round() The Math. round() function returns the value of a number rounded to the nearest integer.


1 Answers

Use the Math.ceil[MDN] function

var n = 4.3; alert(Math.ceil(n)); //alerts 5 
like image 81
Peter Olson Avatar answered Oct 06 '22 00:10

Peter Olson