0

I have a function from excel that I use: =FLOOR(value,0.5)

With this function, I round DOWN to the nearest half value. For example:

10.55 becomes 10.5 10.99 becomes 10.5 10.2 becomes 10

Etc.

Is there an equivalent in javascript? I know that Math.floor () will round down to the nearest integer. However, does anyone know of a good way to round down to the nearest 0.5 instead?

Thank you in advance

1 Answers1

2

Here's your function:

function slipFloor(num){
  let f = Math.floor(num);
  if(num-f < 0.5){
    return f;
  }
  return f+0.5;
}
console.log(slipFloor(10.55));
console.log(slipFloor(10.99));
console.log(slipFloor(10.2));
StackSlave
  • 10,613
  • 2
  • 18
  • 35