[英]Modify floor/ceiling function to work with numbers other than 1?
默認情況下,floor函數總是向下舍入,ceil函數總是向上舍入到最接近的'1'
我如何向下/向上舍入到最近的20,或最接近的1000?
假設我有一個數字x和一個因子y。
我想找到y與x的最接近的因子。
我用它來查找圖表的最大值和最小值。 謝謝。
簡單:
var z = y * Math.floor(x / y);
Math.floor(x / 20) * 20;
Math.ceil(x / 20) * 20;
在你的情況下:
Math.floor(x / y) * y;
x / y
將表達式轉換units of y
; 使用.floor()
或.ceil()
然后向下或向上舍入; 之后再次乘以y
得到你的最終答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.