简体   繁体   中英

Excel ceiling function in JavaScript

How to write Excel CEILING(number, significance) function in JavaScript or in decimal.js

CEILING(10,3) = 12 round up to nearest 3

CEILING(36,7) = 42 round up to nearest 7

CEILING(560,100) = 600 round up to nearest 100

CEILING(6.36,0.05) = 6.40 round up to nearest 0.05

Here's a solution:

 function ceiling(number, significance) { return Math.ceil(number / significance) * significance; } window.onload = ()=> { console.log(ceiling(10, 3)); console.log(ceiling(36, 7)); console.log(ceiling(560, 100)); console.log(ceiling(6.36, 0.05)); };

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM