2

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

KevBot
  • 17,900
  • 5
  • 50
  • 68
Manik
  • 33
  • 3
  • Take a look at [this old post](https://stackoverflow.com/questions/1726630/formatting-a-number-with-exactly-two-decimals-in-javascript) where your question has been already answered and well explained. Simply use toFixed() method as `(321.31125).toFixed(2)`. – Kate Orlova Jun 18 '19 at 18:18
  • @KateOrlova using `.toFixed()` does not round the number though, which was what the OP asked. – Nick G Jun 18 '19 at 19:29

3 Answers3

7

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));
Nick G
  • 1,953
  • 12
  • 16
  • That I already did, but I was looking for method if exists which will ceil automatically upto the decimal point I want. – Manik Jun 19 '19 at 18:24
  • @Manik I added a function you could implement yourself to do this. To the best of my knowledge though, no functions are built in that would allow you to do this. – Nick G Jun 20 '19 at 11:52
  • Thank you .. it really helps – Manik Jun 21 '19 at 15:46
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
bhavesh
  • 453
  • 3
  • 11
  • you can also use ` let a = "-36.3".split("."); parseFloat(a[0] + "." + (a[1] ? (a[1][0] || "0") + (a[1][1] || "0") : "00")); ` in javascript te calculation for 0.1+0.2 is (1/16+(1/10-1/16))+(2/16+(2/10-2/16)) – bhavesh Dec 03 '19 at 04:45
0

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
Jax297
  • 617
  • 1
  • 6
  • 8
  • That I already did, but I was looking for method if exists which will ceil automatically upto the decimal point I want – Manik Jun 19 '19 at 18:24