Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I round to whole numbers in JavaScript?

I have the following code to calculate a certain percentage:

var x = 6.5; var total;  total = x/15*100;  // Result  43.3333333333 

What I want to have as a result is the exact number 43 and if the total is 43.5 it should be rounded to 44

Is there way to do this in JavaScript?

like image 209
idontknowhow Avatar asked Aug 06 '11 16:08

idontknowhow


People also ask

How do you get a whole number in JavaScript?

Use the Math. round() function to round the result to the nearest integer.

How do you round the number 7.25 to the nearest whole number 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.

How do you round up to a whole number?

To round a number to the nearest whole number, you have to look at the first digit after the decimal point. If this digit is less than 5 (1, 2, 3, 4) we don't have to do anything, but if the digit is 5 or greater (5, 6, 7, 8, 9) we must round up.

How do you round to the lowest whole number in JavaScript?

The Math. floor() method rounds a number DOWN to the nearest integer.


1 Answers

Use the Math.round() function to round the result to the nearest integer.

like image 130
hmakholm left over Monica Avatar answered Sep 28 '22 05:09

hmakholm left over Monica