Search code examples
javascript

Javascript round up to the nearest integer


I'm creating an online store. i'm create calculate heavy goods for shipment, but i have a problem in heavy goods.

I want when total heavy goods example :

3.0 to 3

3.1 to 4

3.2 to 4

untill

3.9 to 4

i'm confused when heavy goods in (3.1 to 3.4) this result is 3 i want change this result to 4

This is my code or JsFiddle

var num = 3.4;
var n = num.toFixed()
alert(n);

Help me, Thank's


Solution

  • Use Math.ceil function.

    var num1 = 3.0;
    var num2 = 3.1;
    var num3 = 3.9;
    
    console.log(Math.ceil(num1));
    console.log(Math.ceil(num2));
    console.log(Math.ceil(num3));