mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-12 05:35:16 -05:00
* Initial * CSS lint * Test CI * Add 1v1, 2v2, and 3v3 Tags (#1771) * Initial * CSS lint * Test CI * Rename step --------- Co-authored-by: xi <104683822+ximk@users.noreply.github.com>
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
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 secondsToMinutes(seconds: number) {
|
|
const minutes = Math.floor(seconds / 60);
|
|
const secondsLeft = seconds % 60;
|
|
return `${minutes}:${secondsLeft.toString().padStart(2, "0")}`;
|
|
}
|
|
|
|
export function secondsToMinutesNumberTuple(seconds: number) {
|
|
const minutes = Math.floor(seconds / 60);
|
|
const secondsLeft = seconds % 60;
|
|
return [minutes, secondsLeft] as const;
|
|
}
|
|
|
|
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;
|
|
}
|