-3

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

0

1 Answer 1

8

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));
    };

Sign up to request clarification or add additional context in comments.

3 Comments

Answers like these, while solving the problem, sadly also encourage more such zero-effort, "give me code" questions.
@Vasan Got it. I'm a newbie here, and that's not going to happen again. Sorry for that.
Let's mention some Excel's Floor function equivalent. return Math.floor(number / significance) * significance;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.