sendou.ink/app/utils/number.ts
Kalle 198010e3e7
Some checks are pending
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
New date picker + VoD form rework (#2055)
* Initial

* Progress

* Form fields fun

* Pov

* Progress

* Some errors

* Progress

* Progress

* Progress

* Progress

* Comment

* Progress

* Remove comment

* Fix editing

* Redundant check
2025-01-26 12:56:19 +02:00

26 lines
738 B
TypeScript

export function roundToNDecimalPlaces(num: number, n = 2) {
return Number((Math.round(num * 10 ** n) / 10 ** n).toFixed(n));
}
export function cutToNDecimalPlaces(num: number, n = 2) {
const multiplier = 10 ** n;
const truncatedNum = Math.trunc(num * multiplier) / multiplier;
const result = truncatedNum.toFixed(n);
return Number(n > 0 ? result.replace(/\.?0+$/, "") : result);
}
export function sumArray(arr: number[]) {
return arr.reduce((acc, curr) => acc + curr, 0);
}
export function averageArray(arr: number[]) {
return sumArray(arr) / arr.length;
}
export function safeNumberParse(value: string | null) {
if (value === null) return null;
const result = Number(value);
return Number.isNaN(result) ? null : result;
}