web: round values and add average

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-12-12 23:13:32 +01:00
parent f6823cd303
commit 13d7ad2a8c
4 changed files with 36 additions and 20 deletions

View File

@@ -0,0 +1,12 @@
export const round = (num: number, decimals = 2) => {
const factor = Math.pow(10, decimals);
return Math.round((num + Number.EPSILON) * factor) / factor;
};
export const average = (arr: number[]) =>
arr.reduce((p, c) => p + c, 0) / arr.length;
export const sum = (arr: number[]) =>
round(arr.reduce((acc, item) => acc + item, 0));
export const isFloat = (n: number) => n % 1 !== 0;