Home > front end >  Round up/down inline style value using jQuery
Round up/down inline style value using jQuery

Time:10-01

Is it possible using jQuery to change the value of an inline style placed on an element using jquery?

I am using Owl Carousel and its rendering width values with 3 decimal places and so if the value is over 0.5 the browser is rounding up and creating an unwanted 1px gap. So if the value is 10.224 thats OK as the browser makes it 10, but if the value (which is calculated by Owl Carousel js) is 10.564 (e.g. above 0.5) the browser makes it 11 and gives an unwanted 1px gap.

I'm looking for a way to make the value, be it, 10.123, 10.875, 10.457 or 10.999 always be 10. This needs to happen after the owl carousel values have been calculated...

Any ideas?

CodePudding user response:

OK, rather than hacking it - Fixed it at the source - there is an issue in the Owl Carousel JS itself - as its rounding to 3 digits. To fix simply edit the Owl Carousel JS file to change references to "ToFixed(3)" to become "ToFixed(0)". This fixes it.

CodePudding user response:

What you're looking for: Math.round(num * 100) / 100.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round

  • Related