Open temkaes opened 4 years ago
The problem is that first num
is divided by divisions
and only then multiplied by i
. Javascript calculates with floats and thus rounds in sometimes unexpected ways. Change the order (first multiply, then divide) and it's gonna be allright. (Or round the result before parseInt
ing.)
My fix...
// For first iteration, don't divide.
var auxNum = i == divisions ? num : num / divisions * i;
then...
// Preserve as int if input was int
var newNum = parseInt(auxNum);
// Preserve float if input was float
if (isFloat) {
newNum = parseFloat(auxNum).toFixed(decimalPlaces);
}
24.0 works, 23 works and 25 works but 24 gets stuck at 23. Interestingly, the same issue can be replicated at 3, 6, 12, 24, 48 and 96...
I tried a few of the earlier posted solutions but still doesn't work.