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));
Answer from Nick G on Stack OverflowVideos
How to get the ceil value after 2 decimal places using JavaScript - Stack Overflow
Newbie here. Is using Math.ceil(Math.random()) a bad idea?
Why not always use Math.round instead of Math.floor?
Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.
When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:
Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.
Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.
Math.floor vs Math.round vs parseInt vs Bitwise jsPerf
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.9999999999995so use for ceiling number