diff --git a/frontend/__tests__/utils/format.spec.ts b/frontend/__tests__/utils/format.spec.ts
index b7ebdd5cdbb57..b2c5d9a6c8d6c 100644
--- a/frontend/__tests__/utils/format.spec.ts
+++ b/frontend/__tests__/utils/format.spec.ts
@@ -90,6 +90,13 @@ describe("format.ts", () => {
expect(format.typingSpeed(null, { suffix: " raw" })).toEqual("-");
expect(format.typingSpeed(undefined, { suffix: " raw" })).toEqual("-");
});
+
+ it("should format with rounding", () => {
+ const format = getInstance({ alwaysShowDecimalPlaces: false });
+ expect(format.typingSpeed(80.25)).toEqual("80");
+ expect(format.typingSpeed(80.25, { rounding: Math.ceil })).toEqual("81");
+ expect(format.typingSpeed(80.75, { rounding: Math.floor })).toEqual("80");
+ });
});
describe("percentage", () => {
it("should format with decimalPlaces from configuration", () => {
@@ -142,6 +149,13 @@ describe("format.ts", () => {
expect(format.percentage(null, { suffix: " raw" })).toEqual("-");
expect(format.percentage(undefined, { suffix: " raw" })).toEqual("-");
});
+
+ it("should format with rounding", () => {
+ const format = getInstance({ alwaysShowDecimalPlaces: false });
+ expect(format.percentage(80.25)).toEqual("80%");
+ expect(format.percentage(80.25, { rounding: Math.ceil })).toEqual("81%");
+ expect(format.percentage(80.75, { rounding: Math.floor })).toEqual("80%");
+ });
});
describe("decimals", () => {
it("should format with decimalPlaces from configuration", () => {
@@ -192,6 +206,13 @@ describe("format.ts", () => {
expect(format.decimals(null, { suffix: " raw" })).toEqual("-");
expect(format.decimals(undefined, { suffix: " raw" })).toEqual("-");
});
+
+ it("should format with rounding", () => {
+ const format = getInstance({ alwaysShowDecimalPlaces: false });
+ expect(format.decimals(80.25)).toEqual("80");
+ expect(format.decimals(80.25, { rounding: Math.ceil })).toEqual("81");
+ expect(format.decimals(80.75, { rounding: Math.floor })).toEqual("80");
+ });
});
});
diff --git a/frontend/src/ts/account/pb-tables.ts b/frontend/src/ts/account/pb-tables.ts
index 4f1bcf0d67fda..e03f14d2729d2 100644
--- a/frontend/src/ts/account/pb-tables.ts
+++ b/frontend/src/ts/account/pb-tables.ts
@@ -156,6 +156,7 @@ function buildPbHtml(
})}
${Format.percentage(pbData.acc, {
showDecimalPlaces: false,
+ rounding: Math.floor,
})}
@@ -164,7 +165,10 @@ function buildPbHtml(
suffix: ` ${speedUnit}`,
})}
${Format.typingSpeed(pbData.raw, { suffix: " raw" })}
- ${Format.percentage(pbData.acc, { suffix: " acc" })}
+ ${Format.percentage(pbData.acc, {
+ suffix: " acc",
+ rounding: Math.floor,
+ })}
${Format.percentage(pbData.consistency, {
suffix: " con",
})}
diff --git a/frontend/src/ts/elements/modes-notice.ts b/frontend/src/ts/elements/modes-notice.ts
index 79fd502d36cec..5efda15ff3181 100644
--- a/frontend/src/ts/elements/modes-notice.ts
+++ b/frontend/src/ts/elements/modes-notice.ts
@@ -158,7 +158,7 @@ export async function update(): Promise {
: "";
const avgAccText = ["acc", "both"].includes(Config.showAverage)
- ? Format.percentage(avgAcc, { suffix: " acc" })
+ ? Format.percentage(avgAcc, { suffix: " acc", rounding: Math.floor })
: "";
const text = `${avgWPMText} ${avgAccText}`.trim();
diff --git a/frontend/src/ts/pages/account.ts b/frontend/src/ts/pages/account.ts
index f524bfb2e9c70..7995aec4874db 100644
--- a/frontend/src/ts/pages/account.ts
+++ b/frontend/src/ts/pages/account.ts
@@ -881,9 +881,15 @@ async function fillContent(): Promise {
$(".pageAccount .highestWpm .mode").html(topMode);
$(".pageAccount .testsTaken .val").text(testCount);
- $(".pageAccount .highestAcc .val").text(Format.percentage(topAcc));
- $(".pageAccount .avgAcc .val").text(Format.percentage(totalAcc / testCount));
- $(".pageAccount .avgAcc10 .val").text(Format.percentage(totalAcc10 / last10));
+ $(".pageAccount .highestAcc .val").text(
+ Format.percentage(topAcc, { rounding: Math.floor })
+ );
+ $(".pageAccount .avgAcc .val").text(
+ Format.percentage(totalAcc / testCount, { rounding: Math.floor })
+ );
+ $(".pageAccount .avgAcc10 .val").text(
+ Format.percentage(totalAcc10 / last10, { rounding: Math.floor })
+ );
if (totalCons === 0 || totalCons === undefined) {
$(".pageAccount .avgCons .val").text("-");
diff --git a/frontend/src/ts/popups/pb-tables-popup.ts b/frontend/src/ts/popups/pb-tables-popup.ts
index 86697a5c549eb..088fb363cf22e 100644
--- a/frontend/src/ts/popups/pb-tables-popup.ts
+++ b/frontend/src/ts/popups/pb-tables-popup.ts
@@ -63,7 +63,9 @@ function update(mode: SharedTypes.Config.Mode): void {
${Format.typingSpeed(pb.wpm)}
- ${Format.percentage(pb.acc)}
+ ${Format.percentage(pb.acc, {
+ rounding: Math.floor,
+ })}
|
${Format.typingSpeed(pb.raw)}
diff --git a/frontend/src/ts/test/result.ts b/frontend/src/ts/test/result.ts
index ac1164ce44953..bb43a68af8a6d 100644
--- a/frontend/src/ts/test/result.ts
+++ b/frontend/src/ts/test/result.ts
@@ -228,7 +228,9 @@ function updateWpmAndAcc(): void {
}
$("#result .stats .raw .bottom").text(Format.typingSpeed(result.rawWpm));
$("#result .stats .acc .bottom").text(
- result.acc === 100 ? "100%" : Format.percentage(result.acc)
+ result.acc === 100
+ ? "100%"
+ : Format.percentage(result.acc, { rounding: Math.floor })
);
if (Config.alwaysShowDecimalPlaces) {
diff --git a/frontend/src/ts/utils/format.ts b/frontend/src/ts/utils/format.ts
index b9c63ae4a616a..ef6c8634fcb90 100644
--- a/frontend/src/ts/utils/format.ts
+++ b/frontend/src/ts/utils/format.ts
@@ -6,12 +6,14 @@ export type FormatOptions = {
showDecimalPlaces?: boolean;
suffix?: string;
fallback?: string;
+ rounding?: (val: number) => number;
};
const FORMAT_DEFAULT_OPTIONS: FormatOptions = {
suffix: "",
fallback: "-",
showDecimalPlaces: undefined,
+ rounding: Math.round,
};
export class Formatting {
@@ -60,7 +62,7 @@ export class Formatting {
) {
return Misc.roundTo2(value).toFixed(2) + suffix;
}
- return Math.round(value).toString() + suffix;
+ return (formatOptions.rounding ?? Math.round)(value).toString() + suffix;
}
}
|