I need help rounding numbers in a specific way:
Value Expected
0,523% 1%
2,235% 2,5%
-0,081% -0,5%
-1,081% -1,5%
How can I achieve this using JavaScript?
Possible Solution:
static round (num) {
const abs = Math.abs(num);
const sign = num < 0 ? -1 : 1;
return sign * (abs % 1 > 0.5 ? Math.ceil(abs) : Math.floor(abs) + 0.5) }
}
I tried using Excel formula =round(Value/Granularity)*Granularity but couldn't get it to work in JavaScript.
static calc(num){
const granularitiy = 0.00005;
let calc = Math.round((num / granularitiy)) * granularitiy;
return calc;
}
The granularity value I am working with is 0.00005;