sendou.ink/app/utils/number.ts
PM.Net JDS 40e56d2b53
Round sub velocity to thousandths place, fix bug in round function (Fixes #1348) (#1353)
* Round velocity to thousandths place, fix bug in round function

* Prettier application
2023-04-22 11:31:35 +03:00

27 lines
877 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 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);
}