Skip to content

Commit c692484

Browse files
committed
normalise floating point values for pips/values/selected
- resolves #58 - also fixes another few small bugs with tiny floating points
1 parent b3ec6d1 commit c692484

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/RangePips.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@
3030
// methods
3131
export let percentOf = undefined;
3232
export let moveHandle = undefined;
33+
export let fixFloat = undefined;
3334
3435
$: pipStep = pipstep || ((max - min) / step >= ( vertical ? 50 : 100 ) ? (max - min) / ( vertical ? 10 : 20 ) : 1);
3536
3637
$: pipCount = parseInt((max - min) / (step * pipStep), 10);
3738
3839
$: pipVal = function(val) {
39-
return min + val * step * pipStep;
40+
return fixFloat( min + val * step * pipStep );
4041
};
4142
4243
$: isSelected = function(val) {
43-
return values.some(v => v === val);
44+
return values.some(v => fixFloat(v) === fixFloat(val));
4445
};
4546
4647
$: inRange = function(val) {
@@ -182,7 +183,7 @@
182183
>
183184
{#if all === 'label' || first === 'label'}
184185
<span class="pipVal">
185-
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(min,0,0)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
186+
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(fixFloat(min),0,0)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
186187
</span>
187188
{/if}
188189
</span>
@@ -220,7 +221,7 @@
220221
>
221222
{#if all === 'label' || last === 'label'}
222223
<span class="pipVal">
223-
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(max,pipCount,100)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
224+
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(fixFloat(max),pipCount,100)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
224225
</span>
225226
{/if}
226227
</span>

src/RangeSlider.svelte

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
5757
let springPositions;
5858
59+
const fixFloat = (v) => parseFloat(v.toFixed(precision));
60+
5961
$: {
6062
6163
// check that "values" is an array, or set it as array
@@ -97,7 +99,7 @@
9799
} else if (perc >= 100) {
98100
return 100;
99101
} else {
100-
return parseFloat(perc.toFixed(precision));
102+
return fixFloat(perc);
101103
}
102104
};
103105
@@ -121,11 +123,10 @@
121123
$: alignValueToStep = function (val) {
122124
// sanity check for performance
123125
if (val <= min) {
124-
return min;
126+
return fixFloat(min);
125127
} else if (val >= max) {
126-
return max;
128+
return fixFloat(max);
127129
}
128-
129130
// find the middle-point between steps
130131
// and see if the value is closer to the
131132
// next step, or previous step
@@ -139,8 +140,7 @@
139140
// make sure the returned value is set to the precision desired
140141
// this is also because javascript often returns weird floats
141142
// when dealing with odd numbers and percentages
142-
143-
return parseFloat(aligned.toFixed(precision));
143+
return fixFloat(aligned);
144144
};
145145
146146
/**
@@ -854,6 +854,7 @@
854854
{focus}
855855
{percentOf}
856856
{moveHandle}
857+
{fixFloat}
857858
/>
858859
{/if}
859860
</div>

test/src/App.svelte

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
boundSlider.style.outlineOffset = "15px";
5757
},500);
5858
});
59+
60+
let decimals = [0.003, 0.123];
61+
let decimals2 = [-0.133, 0.444444444];
5962
6063
</script>
6164

@@ -157,6 +160,7 @@
157160
<br>
158161
<RangeSlider float pips step={10} pipstep={1} {reversed} {hoverable} {disabled} />
159162
<RangeSlider float pips step={10} pipstep={2} {reversed} {hoverable} {disabled} />
163+
<RangeSlider float pips step={10} pipstep={0.25} {reversed} {hoverable} {disabled} />
160164
<RangeSlider float pips step={0.1} min={dynamic[0]} max={dynamic[1]}
161165
on:start={(e) => { console.log("start",e.detail)}}
162166
on:stop={(e) => { console.log("stop",e.detail)}}
@@ -223,6 +227,13 @@
223227
{/each}
224228
<hr>
225229

230+
<h2>Decimal / Float Values</h2>
231+
<RangeSlider bind:values={decimals} float pips all="label" min={-0.01} max={0.01} step={0.0005} precision={3} {reversed} {hoverable} {disabled} />
232+
{decimals}<br>
233+
234+
<RangeSlider bind:values={decimals2} float pips all="label" min={-0.01} max={0.01} step={0.0005} precision={5} {reversed} {hoverable} {disabled} />
235+
{decimals2}<br>
236+
226237
</div>
227238

228239

0 commit comments

Comments
 (0)