diff options
| author | Maze Winther <mazewinther@gmail.com> | 2026-03-27 21:24:59 +0100 |
|---|---|---|
| committer | Maze Winther <mazewinther@gmail.com> | 2026-03-29 15:58:46 +0200 |
| commit | c34e01c63eea9fb97d8d4e93546d884b0294e475 (patch) | |
| tree | 078c5e28d93bc7c4dc6a6a4818d14a67713192aa /apps/web/src/utils | |
| parent | 3e75852ffee06ec75006c787389870107395bd2d (diff) | |
| download | opencut-c34e01c63eea9fb97d8d4e93546d884b0294e475.tar.gz | |
refactor: clean up number formatting and step handling in property tabs
Diffstat (limited to 'apps/web/src/utils')
| -rw-r--r-- | apps/web/src/utils/math.ts | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/apps/web/src/utils/math.ts b/apps/web/src/utils/math.ts index 6e912b43..8043871f 100644 --- a/apps/web/src/utils/math.ts +++ b/apps/web/src/utils/math.ts @@ -22,7 +22,7 @@ export function clampRound({ return Math.round(clamp({ value, min, max }));
}
-function getFractionDigitsForStep({ step }: { step: number }): number {
+export function getFractionDigitsForStep({ step }: { step: number }): number {
const normalizedStep = step.toString().toLowerCase();
if (normalizedStep.includes("e-")) {
return Number(normalizedStep.split("e-")[1] ?? 0);
@@ -59,12 +59,43 @@ export function isNearlyEqual({ export function formatNumberForDisplay({
value,
+ fractionDigits,
+ minFractionDigits = 0,
maxFractionDigits = 6,
}: {
value: number;
+ fractionDigits?: number;
+ minFractionDigits?: number;
maxFractionDigits?: number;
}): string {
- return Number(value.toFixed(maxFractionDigits)).toString();
+ const resolvedMaxFractionDigits = Math.max(
+ 0,
+ fractionDigits ?? maxFractionDigits,
+ );
+ const resolvedMinFractionDigits = Math.min(
+ Math.max(0, fractionDigits ?? minFractionDigits),
+ resolvedMaxFractionDigits,
+ );
+ const fixedValue = value.toFixed(resolvedMaxFractionDigits);
+
+ if (resolvedMaxFractionDigits === 0) {
+ return Number(fixedValue) === 0 ? "0" : fixedValue;
+ }
+
+ const [integerPart, fractionPart = ""] = fixedValue.split(".");
+ const normalizedIntegerPart = Number(fixedValue) === 0 ? "0" : integerPart;
+ let trimmedFractionPart = fractionPart;
+
+ while (
+ trimmedFractionPart.length > resolvedMinFractionDigits &&
+ trimmedFractionPart.endsWith("0")
+ ) {
+ trimmedFractionPart = trimmedFractionPart.slice(0, -1);
+ }
+
+ return trimmedFractionPart
+ ? `${normalizedIntegerPart}.${trimmedFractionPart}`
+ : normalizedIntegerPart;
}
export function evaluateMathExpression({
|
