抱歉标题,在标题中解释这个问题有点困难。
所以我想把一个数字四舍五入到它最近的比例/因子(总是向上)或者当它添加到文本字段时如何调用它。我想用 javascript 来做这件事,但找不到一个函数或例子,但我希望这个解决方案有一个函数?
例子:
Scale/factor = 12
User enters the number 3 , the number should change into 12
User enters the number 25, the number should change into 36
User enters the number 47, the number should change into 48
只需Math.ceil
将除法的结果四舍五入并将其与您的因子相乘:
const factor = 12;
Math.ceil(47 / factor) * factor; // 48
Math.ceil(25 / factor) * factor; // 36
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句