This should do it:
Math.ceil(N / 10) * 10;
Where N is one of your numbers.
Answer from alexn on Stack OverflowThis should do it:
Math.ceil(N / 10) * 10;
Where N is one of your numbers.
To round a number to the next greatest multiple of 10, add one to the number before getting the Math.ceil of a division by 10. Multiply the result by ten.
Math.ceil((n+1)/10)*10;
1->10
2->10
3->10
4->10
5->10
6->10
7->10
8->10
9->10
10->20
11->20
12->20
13->20
14->20
15->20
16->20
17->20
18->20
19->20
20->30
21->30
22->30
23->30
24->30
25->30
26->30
27->30
28->30
29->30
30->40
35-> 40
40-> 50
45-> 50
50-> 60
55-> 60
60-> 70
65-> 70
70-> 80
75-> 80
80-> 90
85-> 90
90-> 100
95-> 100
100-> 110
Videos
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 = 100vs.Math.ceil(60/100)*100 = 100
Math.round(40/100)*100 = 0vs.Math.ceil(40/100)*100 = 100
Math.round(-60/100)*100 = -100vs.Math.ceil(-60/100)*100 = -0
Math.round(-40/100)*100 = -0vs.Math.ceil(-40/100)*100 = -0
This will do what you want:
Math.round(value/1000)*1000
examples:
Math.round(1001/1000)*1000
1000
Math.round(1004/1000)*1000
1000
Math.round(1500/1000)*1000
2000
By using ES3 Number method, it performs a rounding if no decimal place defined.
(value / 1000).toFixed() * 1000
The original answer was:
(value / 1000).toFixed(3) * 1000;
Yet this is incorrect, due to the value will return the exact original number, instead of affecting the ceil/floor on the value.
Method 1: The quick way is to use toFixed() method like this:
var num = 2.12;
var round = num.toFixed(1); // will out put 2.1 of type String
One thing to note here is that it would round 2.12 to 2.1 and 2.15 to 2.2
Method 2: On the other hand you can use Math.round with this trick:
var num = 2.15;
Math.round(num * 10) / 10; // would out put 2.2
It would round to the upper bound.
So, choose whichever you like.
Also if you use a modern version of JS ie. ES then using const and let instead for variable declaration might be a better approach.
NOTE: remember that .toFixed() returns a string. If you want a number, use the Math.round() approach. Thanks for the reminder @pandubear
Math.round(X); // round X to an integer
Math.round(10*X)/10; // round X to tenths
Math.round(100*X)/100; // round X to hundredths
Math.round(1000*X)/1000; // round X to thousandths