diff --git a/packages/calcite-components/src/components/slider/slider.e2e.ts b/packages/calcite-components/src/components/slider/slider.e2e.ts index c231e9a28a0..fd09f5308f5 100644 --- a/packages/calcite-components/src/components/slider/slider.e2e.ts +++ b/packages/calcite-components/src/components/slider/slider.e2e.ts @@ -204,6 +204,19 @@ describe("calcite-slider", () => { }); }); + // skipped due to a bug where value is rounded down instead of up: + // https://github.com/Esri/calcite-design-system/issues/9684 + it.skip("step floating point precision", async () => { + const page = await newE2EPage(); + await page.setContent( + html``, + ); + const slider = await page.find("calcite-slider"); + + await page.waitForChanges(); + expect((await slider.getProperty("value")).toString()).toBe("1.4"); + }); + it("only selects values on step interval when snap prop is passed", async () => { const page = await newE2EPage(); await page.setContent(` diff --git a/packages/calcite-components/src/components/slider/slider.tsx b/packages/calcite-components/src/components/slider/slider.tsx index 31190af0d98..afede983293 100644 --- a/packages/calcite-components/src/components/slider/slider.tsx +++ b/packages/calcite-components/src/components/slider/slider.tsx @@ -46,6 +46,7 @@ import { import { clamp, decimalPlaces } from "../../utils/math"; import { ColorStop, DataSeries } from "../graph/interfaces"; import { Scale } from "../interfaces"; +import { BigDecimal } from "../../utils/number"; import { CSS, maxTickElementThreshold } from "./resources"; import { ActiveSliderProperty, SetValueProperty, SideOffset, ThumbType } from "./interfaces"; @@ -964,8 +965,14 @@ export class Slider */ private getClosestStep(value: number): number { const { max, min, step } = this; - let snappedValue = Math.floor((value - min) / step) * step + min; - snappedValue = Math.min(Math.max(snappedValue, min), max); + + // prevents floating point precision issues + const bigDecimalString = new BigDecimal(`${Math.floor((value - min) / step)}`) + .multiply(`${step}`) + .add(`${min}`) + .toString(); + + let snappedValue = this.clamp(Number(bigDecimalString)); if (snappedValue > max) { snappedValue -= step;