How to get the ceil value after 2 decimal places using JavaScript

Viewed 1179

I want to achieve the following:

suppose x = 321.31125

Now I want the ceil value after the 2nd decimal point ... so the expected result should be 321.32

Note: I know rounding which will return 321.31 but I want 321.32

3 Answers

You can just multiply by 100, take the ceiling of that new number (32131.125 => 32132), then divide that by 100.

var x = 321.31125
console.log(Math.ceil(x * 100) / 100)

Edit: If you are looking to create a function, you could always do something like this post details:

function roundUp(num, precision) {
    precision = Math.pow(10, precision)
    return Math.ceil(num * precision) / precision
}

console.log(roundUp(321.31125, 2));

You must have to try this

Math.ceil(Math.round(-36.3 * 100)) / 100

in javascript there is 0.1+0.2 === 0.3 is **false

for -36.3 you have to round that calculation so

    -36.3 * 100=-3629.9999999999995  
so use for ceiling number

You could multiply by 100, ceil that value and divide by 100 again, ie

321.31125 * 100 = 32131.125
ceil 32131.125 = 32132.0
32132.0 / 100 = 321.32
Related