Skip to content

Commit 2552e2a

Browse files
authored
(bug)(webapp) support negative number (#2284)
1 parent 4bfa10b commit 2552e2a

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

webapp/packages/chat-sdk/src/utils/utils.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function formatByDecimalPlaces(value: number | string, decimalPlaces: num
1010
return value;
1111
}
1212
let strValue = (+value).toFixed(decimalPlaces);
13-
if (!/^[0-9.]+$/g.test(strValue)) {
13+
if (!/^-?[0-9.]+$/g.test(strValue)) {
1414
return '0';
1515
}
1616
while (strValue.includes('.') && (strValue.endsWith('.') || strValue.endsWith('0'))) {
@@ -72,17 +72,20 @@ export const getFormattedValue = (value: number | string, remainZero?: boolean)
7272
if (!isFinite(+value)) {
7373
return value;
7474
}
75+
76+
const absNumericValue = Math.abs(+value);
77+
7578
const unit =
76-
+value >= 100000000
79+
absNumericValue >= 100000000
7780
? NumericUnit.OneHundredMillion
78-
: +value >= 10000
81+
: absNumericValue >= 10000
7982
? NumericUnit.TenThousand
8083
: NumericUnit.None;
8184

8285
let formattedValue = formatByUnit(value, unit);
8386
formattedValue = formatByDecimalPlaces(
8487
formattedValue,
85-
unit === NumericUnit.OneHundredMillion ? 2 : +value < 1 ? 3 : 1
88+
unit === NumericUnit.OneHundredMillion ? 2 : absNumericValue < 1 ? 3 : 1
8689
);
8790
formattedValue = formatByThousandSeperator(formattedValue);
8891
if ((typeof formattedValue === 'number' && isNaN(formattedValue)) || +formattedValue === 0) {
@@ -93,7 +96,7 @@ export const getFormattedValue = (value: number | string, remainZero?: boolean)
9396

9497
export const formatNumberWithCN = (num: number) => {
9598
if (isNaN(num)) return '-';
96-
if (num >= 10000) {
99+
if (Math.abs(+num) >= 10000) {
97100
return (num / 10000).toFixed(1) + '万';
98101
} else {
99102
return formatByDecimalPlaces(num, 2);

webapp/packages/supersonic-fe/src/utils/utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export function formatByDecimalPlaces(value: number | string, decimalPlaces: num
268268
return value;
269269
}
270270
let str = (+value).toFixed(decimalPlaces);
271-
if (!/^[0-9.]+$/g.test(str)) {
271+
if (!/^-?[0-9.]+$/g.test(str)) {
272272
return '0';
273273
}
274274
while (str.includes('.') && (str.endsWith('.') || str.endsWith('0'))) {
@@ -337,17 +337,20 @@ export const getFormattedValueData = (value: number | string, remainZero?: boole
337337
if (!isFinite(+value)) {
338338
return value;
339339
}
340+
341+
const absNumericValue = Math.abs(+value);
342+
340343
const unit =
341-
value >= 100000000
344+
absNumericValue >= 100000000
342345
? NumericUnit.OneHundredMillion
343-
: value >= 10000
346+
: absNumericValue >= 10000
344347
? NumericUnit.EnTenThousand
345348
: NumericUnit.None;
346349

347350
let formattedValue = formatByUnit(value, unit);
348351
formattedValue = formatByDecimalPlaces(
349352
formattedValue,
350-
unit === NumericUnit.OneHundredMillion ? 2 : value < 1 ? 3 : 1,
353+
unit === NumericUnit.OneHundredMillion ? 2 : absNumericValue < 1 ? 3 : 1,
351354
);
352355
formattedValue = formatByThousandSeperator(formattedValue);
353356
if ((typeof formattedValue === 'number' && isNaN(formattedValue)) || +formattedValue === 0) {

0 commit comments

Comments
 (0)