Skip to content

Commit 385c57c

Browse files
authored
Merge pull request #818 from NemeZZiZZ/fix/formatbignumber-negative-and-boundary
fix: format negative values and exact magnitude boundaries in formatBigNumber
2 parents 68bed6b + 5cdc5ba commit 385c57c

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

src/common/utils/format.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,16 @@ export function formatPrecision(value: string | number, precision?: number): str
102102
export function formatBigNumber(value: string | number): string {
103103
const v = +value
104104
if (isNumber(v)) {
105-
if (v > 1000000000) {
106-
return `${+(v / 1000000000).toFixed(3)}B`
105+
const sign = v < 0 ? '-' : ''
106+
const a = Math.abs(v)
107+
if (a >= 1000000000) {
108+
return `${sign}${+(a / 1000000000).toFixed(3)}B`
107109
}
108-
if (v > 1000000) {
109-
return `${+(v / 1000000).toFixed(3)}M`
110+
if (a >= 1000000) {
111+
return `${sign}${+(a / 1000000).toFixed(3)}M`
110112
}
111-
if (v > 1000) {
112-
return `${+(v / 1000).toFixed(3)}K`
113+
if (a >= 1000) {
114+
return `${sign}${+(a / 1000).toFixed(3)}K`
113115
}
114116
}
115117
return `${value}`

0 commit comments

Comments
 (0)