feat: implement percentage calculation utility and refactor analytics to use it

- Added `calcPercentageWithRounding` function for consistent percentage calculations with rounding.
- Updated course and user analytics to utilize the new utility for progress and average grade calculations.
- Removed redundant rounding logic from analytics functions.
This commit is contained in:
Rotimi Best
2025-09-18 10:34:54 +01:00
parent bcd17dd0df
commit a6880f1b54
3 changed files with 31 additions and 19 deletions
@@ -0,0 +1,10 @@
export function calcPercentageWithRounding(a: number, b: number) {
if (b === 0) {
return 0;
}
const rawPercentage = (a / b) * 100;
const fixedString = rawPercentage.toFixed(1);
const roundedNumber = parseFloat(fixedString);
return isNaN(roundedNumber) ? 0 : roundedNumber;
}