Skip to content

Commit b9bde73

Browse files
committed
refactor: change commify function
1 parent 4d8a007 commit b9bde73

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

web/src/utils/commify.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
export const commify = (value: number | bigint | string): string => {
22
const [integerPart, decimalPart] = value.toString().split(".");
33

4-
const formattedIntegerPart = integerPart.replace(/\d(?=(\d{3})+$)/g, "$&,");
4+
let formattedIntegerPart = "";
5+
let counter = 0;
6+
7+
for (let i = integerPart.length - 1; i >= 0; i--) {
8+
counter++;
9+
formattedIntegerPart = integerPart[i] + formattedIntegerPart;
10+
if (counter % 3 === 0 && i !== 0) {
11+
formattedIntegerPart = "," + formattedIntegerPart;
12+
}
13+
}
514

615
return decimalPart ? `${formattedIntegerPart}.${decimalPart}` : formattedIntegerPart;
716
};

0 commit comments

Comments
 (0)