I want to round a number to the nearest 0.5. Not every factor of 0.5, just the 0.5s.
For example, 0.5, 1.5, 2.5, -1.5, -2.5. NOT 1, 1.5, 2, 2.5.
I'm confusing myself just explaining it, so here are some examples of expected outputs.
0.678 => 0.5
0.999 => 0.5
1.265 => 1.5
-2.74 => -2.5
-19.2 => -19.5
I have tried the following code with no luck,
let x = 1.296;
let y = Math.round(x);
let z = y + Math.sign(y) * .5; // 1.5 (Correct!)
let x = -2.6;
let y = Math.round(x);
let z = y + Math.sign(y) * .5; // -3.5 (WRONG, should be -2.5)
The code makes sense in my head, but dosen't work for negative numbers. What am I missing that would make this work?