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?


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


//method 1
Math.ceil(); // rounds up
Math.floor(); // rounds down
Math.round(); // does method 2 in 1 call

//method 2
var number = 1.5; //float
var a = parseInt(number); // to int
number -= a; // get numbers on right of decimal

if(number < 0.5) // if less than round down
    round_down();
else // round up if more than
    round_up();

either one or a combination will solve your question


total = Math.round(total);

Should do it.