By default the floor function always rounds down and the ceil function always rounds up to the nearest '1'
How would I round down/up to the nearest 20, or the nearest 1,000?
Say I had a number x and a factor y.
I want to find the closest factor of y to x.
I am using this to find maximum and minimum values for a graph. Thanks.
简单:
var z = y * Math.floor(x / y);
Math.floor(x / 20) * 20;
Math.ceil(x / 20) * 20;
In your case:
Math.floor(x / y) * y;
The x / y
turns the expression into units of y
; with .floor()
or .ceil()
you then round it down or up; afterwards you multiply with y
again to get your final answer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.