refactor(shared): update utilities, API layer, and types
This commit is contained in:
@@ -1,19 +1,25 @@
|
||||
import { getDecimalPlaces } from '$shared/lib/utils';
|
||||
|
||||
/**
|
||||
* Round a value to the precision of the given step
|
||||
* Rounds a value to match the precision of a given step
|
||||
*
|
||||
* This fixes floating-point precision errors that occur with decimal steps.
|
||||
* For example, with step=0.05, adding it repeatedly can produce values like
|
||||
* 1.3499999999999999 instead of 1.35.
|
||||
* Fixes floating-point precision errors that occur with decimal arithmetic.
|
||||
* For example, repeatedly adding 0.05 can produce 1.3499999999999999
|
||||
* instead of 1.35 due to IEEE 754 floating-point representation.
|
||||
*
|
||||
* We use toFixed() to round to the appropriate decimal places instead of
|
||||
* Math.round(value / step) * step, which doesn't always work correctly
|
||||
* due to floating-point arithmetic errors.
|
||||
* Uses toFixed() instead of Math.round() for correct decimal rounding.
|
||||
*
|
||||
* @param value - The value to round
|
||||
* @param step - The step to round to (defaults to 1)
|
||||
* @param step - The step size to match precision of (default: 1)
|
||||
* @returns The rounded value
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* roundToStepPrecision(1.3499999999999999, 0.05); // 1.35
|
||||
* roundToStepPrecision(1.2345, 0.01); // 1.23
|
||||
* roundToStepPrecision(1.2345, 0.1); // 1.2
|
||||
* roundToStepPrecision(1.5, 1); // 2
|
||||
* ```
|
||||
*/
|
||||
export function roundToStepPrecision(value: number, step: number = 1): number {
|
||||
if (step <= 0) {
|
||||
|
||||
Reference in New Issue
Block a user