Skip to content

Commit 4ae1b43

Browse files
authored
fix(slider): max value had js floating point issues
Sometimes a calculation of max value or number of labels went into JS floating point issues while dividing
1 parent 26b15d2 commit 4ae1b43

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/definitions/modules/slider.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,8 @@
674674
var
675675
step = module.get.step(),
676676
min = module.get.min(),
677-
quotient = step === 0 ? 0 : Math.floor((settings.max - min) / step),
677+
precision = module.get.precision(),
678+
quotient = step === 0 ? 0 : Math.floor(Math.round(((settings.max - min) / step) * precision) / precision),
678679
remainder = step === 0 ? 0 : (settings.max - min) % step
679680
;
680681

@@ -684,7 +685,9 @@
684685
return settings.step;
685686
},
686687
numLabels: function () {
687-
var value = Math.round((module.get.max() - module.get.min()) / (module.get.step() === 0 ? 1 : module.get.step()));
688+
var step = module.get.step(),
689+
precision = module.get.precision(),
690+
value = Math.round(((module.get.max() - module.get.min()) / (step === 0 ? 1 : step)) * precision) / precision;
688691
module.debug('Determined that there should be ' + value + ' labels');
689692

690693
return value;
@@ -699,7 +702,9 @@
699702

700703
switch (settings.labelType) {
701704
case settings.labelTypes.number: {
702-
return Math.round(((value * (module.get.step() === 0 ? 1 : module.get.step())) + module.get.min()) * precision) / precision;
705+
var step = module.get.step();
706+
707+
return Math.round(((value * (step === 0 ? 1 : step)) + module.get.min()) * precision) / precision;
703708
}
704709
case settings.labelTypes.letter: {
705710
return alphabet[value % 26];

0 commit comments

Comments
 (0)