By default <RangeSlider /> will use a precision of 2 decimal places. This controls
the values and the display of pips/floats.
It is necessary to control the precision due to the nature of floating point artihmetic
in javascript. Without a precision check, values on the slider would end up with rounding errors
in certain scenarios (specifically sliders with non-integer steps).
Default Precision
Here you can see the default precision of 2 decimal places.
<script>let value =0.55;</script><RangeSliderstep={0.05} pipstep={2} max={1} pipsall="label"floatbind:value />
step has too much precision
It’s possible to have steps of more than 2 decimal places, but here you can see that even though the
slider is set to a step of 0.005, the values are rounded to 2 decimal places by default.
The slider accepts the value prop as 0.555, but as soon as it is interacted with, it will be rounded to 0.56.
<script>let value =0.555;</script><RangeSliderstep={0.005} pipstep={20} max={1} pipsall="label"floatbind:value />
Changing the precision
So when you have a step with more than 2 decimal places, you should set
the precision to the number of decimal places you want to use.
<RangeSliderstep={0.0001} precision={4} />
Video 😬
<script>// settings for the sliderconstprecision=4;conststep=0.0001;// bindings for the video element and its propertieslet videoElement;$: duration = videoElement?.duration;let currentTime =0;let buffers = [];let bufferStyle ='';/* formatting the float as time (mm:ss.ss) with 2 decimal places, even though precision is set to 4 */constminuteFormat=new Intl.NumberFormat('en', { minimumIntegerDigits: 1 });constsecondFormat=new Intl.NumberFormat('en', { minimumIntegerDigits: 2, minimumFractionDigits: 2, maximumFractionDigits: 2 });consttimeFormat= (v) =>`${minuteFormat.format(Math.floor(v/60))}:${secondFormat.format(v%60).split('.')[0]}<small>.${secondFormat.format(v%60).split('.')[1]}</small>`;/* I'm showing the buffered ranges as a gradient background */constupdateBufferStyle= (e) => { buffers = [];if( !duration ) return;for (let i =0; i < videoElement.buffered.length; i++) {conststart= videoElement.buffered.start(i) / duration *100;constend= videoElement.buffered.end(i) / duration *100; buffers = [...buffers, { start, end }]; } bufferStyle =`background-image: linear-gradient(to right, transparent, ${buffers.map((range, index) =>`transparent ${range.start}%, #94b28d ${range.start}%, #94b28d ${range.end}%, transparent ${range.end}%, transparent` ).join(', ')})`; };</script><RangeSliderid="seek-slider"bind:value={currentTime} {step}{precision}max={duration} floatrange='min'formatter={timeFormat}style={bufferStyle}/><videobind:this={videoElement}src="https://res.cloudinary.com/simey/video/upload/v1749837864/hxpk2grhbvec0hcitnua.mp4"controlsbind:currentTimebind:durationon:seeked={updateBufferStyle}on:play={updateBufferStyle}on:progress={updateBufferStyle}/>