Javascript: What should I use here to achieve my value (round,floor,ceil)

I have different value like

5.5
13.56
45.70
58.89 (never go more than 60)
and many more ...

Suppose they are minutes. And I want their output in the round of nearest 15 division like

5.5 => 0
13.56 => 15
45.70 => 45
58.89 => 60

But I am not sure how can I achieve this rounded output. Please help me...

Answers 1

  • Another option:

    function roundToFifteen(num) {
      var mod = num % 15;
    
      if (mod < 7.5) {
        return num - mod;
      } else {
        return num + (15 - mod);
      }
    }
    

Related Articles