I want to round up to the nearest 100 all the time whether or not the value is 101 or 199 it should round up to 200. For example:
var number = 1233; //use something like Math.round() to round up to always 1300
I'd like to always round up to the nearest 100, never round down, using jQuery.
Rounding to the nearest 100 To round a number to the nearest 100, look at the tens digit. If the tens digit is 5 or more, round up. If the tens digit is 4 or less, round down. The tens digit in 3281 is 8.
ceil() The Math. ceil() function always rounds a number up to the next largest integer.
Use the toFixed() method to round a number to 2 decimal places, e.g. const result = num. toFixed(2) . The toFixed method will round and format the number to 2 decimal places.
Use Math.ceil()
, if you want to always round up:
Math.ceil(number/100)*100
To round up and down to the nearest 100 use Math.round
:
Math.round(number/100)*100
round
vs. ceil
:
Math.round(60/100)*100 = 100
vs.Math.ceil(60/100)*100 = 100
Math.round(40/100)*100 = 0
vs.Math.ceil(40/100)*100 = 100
Math.round(-60/100)*100 = -100
vs.Math.ceil(-60/100)*100 = -0
Math.round(-40/100)*100 = -0
vs.Math.ceil(-40/100)*100 = -0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With