mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2024-11-10 08:55:37 +08:00
fix: too many decimals showing on profile page for some users (fehmer) (#5433)
This commit is contained in:
parent
57b06eff18
commit
b19ff0d9bd
2 changed files with 50 additions and 1 deletions
49
frontend/__tests__/utils/numbers.spec.ts
Normal file
49
frontend/__tests__/utils/numbers.spec.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
import * as Numbers from "../../src/ts/utils/numbers";
|
||||
|
||||
describe("numbers", () => {
|
||||
describe("abbreviateNumber", () => {
|
||||
it("should round to one decimal by default", () => {
|
||||
expect(Numbers.abbreviateNumber(1)).toEqual("1.0");
|
||||
expect(Numbers.abbreviateNumber(1.5)).toEqual("1.5");
|
||||
expect(Numbers.abbreviateNumber(1.55)).toEqual("1.6");
|
||||
|
||||
expect(Numbers.abbreviateNumber(1000)).toEqual("1.0k");
|
||||
expect(Numbers.abbreviateNumber(1010)).toEqual("1.0k");
|
||||
expect(Numbers.abbreviateNumber(1099)).toEqual("1.1k");
|
||||
});
|
||||
it("should round to full numbers", () => {
|
||||
expect(Numbers.abbreviateNumber(1, 0)).toEqual("1");
|
||||
expect(Numbers.abbreviateNumber(1.5, 0)).toEqual("2");
|
||||
expect(Numbers.abbreviateNumber(1.55, 0)).toEqual("2");
|
||||
|
||||
expect(Numbers.abbreviateNumber(1000, 0)).toEqual("1k");
|
||||
expect(Numbers.abbreviateNumber(1010, 0)).toEqual("1k");
|
||||
expect(Numbers.abbreviateNumber(1099, 0)).toEqual("1k");
|
||||
});
|
||||
|
||||
it("should round to two decimals", () => {
|
||||
expect(Numbers.abbreviateNumber(1, 2)).toEqual("1.00");
|
||||
expect(Numbers.abbreviateNumber(1.5, 2)).toEqual("1.50");
|
||||
expect(Numbers.abbreviateNumber(1.55, 2)).toEqual("1.55");
|
||||
|
||||
expect(Numbers.abbreviateNumber(1000, 2)).toEqual("1.00k");
|
||||
expect(Numbers.abbreviateNumber(1010, 2)).toEqual("1.01k");
|
||||
expect(Numbers.abbreviateNumber(1099, 2)).toEqual("1.10k");
|
||||
});
|
||||
it("should use suffixes", () => {
|
||||
let number = 1;
|
||||
expect(Numbers.abbreviateNumber(number)).toEqual("1.0");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0k");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0m");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0b");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0t");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0q");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0Q");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0s");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0S");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0o");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0n");
|
||||
expect(Numbers.abbreviateNumber((number *= 1000))).toEqual("1.0d");
|
||||
});
|
||||
});
|
||||
});
|
|
@ -146,7 +146,7 @@ export function getNumberWithMagnitude(num: number): {
|
|||
*/
|
||||
export function abbreviateNumber(num: number, decimalPoints = 1): string {
|
||||
if (num < 1000) {
|
||||
return num.toString();
|
||||
return num.toFixed(decimalPoints);
|
||||
}
|
||||
|
||||
const exp = Math.floor(Math.log(num) / Math.log(1000));
|
||||
|
|
Loading…
Reference in a new issue