Slider will round float values in `.setValue()`

I have a slider with a range of (0, 2, 0.01). While the float number can be stored to the settings file correctly, when I open up the settings tab again, the slider will load the float value, but display it after some rounding.

This is only a UI stuff, and the actual value in the settings file is untouched.

Why not use a slider from 0, 200 with an increment of 1?

Then you’re not dealing with floats at all, you’re dealing with integers.

Yep I used to do this. I even append a text label insteadof using the dynamic text to host the actual value back. But the conversion code looks nasty lol

Hope someone would fix this.

By default, the slider tooltip will round values to 2 decimals. Is that what you’re referring to?

Oh it’s not a tooltip issue.

I use the slider to adjust the scale of images. It works correctly for the first adjust, and save a float value to my data.json.

However, if I close down and reopen the settings tab, the constructor would call setValue to pass the current scale value to the slider, and here comes the problem. A float value from the local storage will be rounded by the slider to a integer, and then display. Both the tooltip and the position show that there is a rounding when executing setValue(). You can pass some consts to reproduce this.

local → display
0.36 → 0.00
0.63 → 1.00
1.45 → 1.00
1.61 → 2.00

const scaleSlider = new SliderComponent(scaleSetting.controlEl)
	.setValue(this.plugin.settings.scale ?? 1.0)
	.setLimits(0.0, 2, 0.01)
	.setDynamicTooltip()
	.onChange(async (value) => {
		this.plugin.settings.scale = value;
		await this.plugin.saveSettings();
		onSettingsChange(); // update a render preview element on settings page
		value === 0 ? unifyImageWidth() : unifyBondLength();
	});

hmm, what happens if you call setLimits first, before setValue. Does that work?

Yeah it works. I should have tried this. Many thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.