(bug)(webapp) support negative number (#2284)
Some checks failed
supersonic CentOS CI / build (21) (push) Has been cancelled
supersonic mac CI / build (21) (push) Has been cancelled
supersonic ubuntu CI / build (21) (push) Has been cancelled
supersonic windows CI / build (21) (push) Has been cancelled

This commit is contained in:
FredTsang
2025-06-03 20:34:19 +08:00
committed by GitHub
parent 4bfa10ba7c
commit 2552e2ae4b
2 changed files with 15 additions and 9 deletions

View File

@@ -10,7 +10,7 @@ export function formatByDecimalPlaces(value: number | string, decimalPlaces: num
return value;
}
let strValue = (+value).toFixed(decimalPlaces);
if (!/^[0-9.]+$/g.test(strValue)) {
if (!/^-?[0-9.]+$/g.test(strValue)) {
return '0';
}
while (strValue.includes('.') && (strValue.endsWith('.') || strValue.endsWith('0'))) {
@@ -72,17 +72,20 @@ export const getFormattedValue = (value: number | string, remainZero?: boolean)
if (!isFinite(+value)) {
return value;
}
const absNumericValue = Math.abs(+value);
const unit =
+value >= 100000000
absNumericValue >= 100000000
? NumericUnit.OneHundredMillion
: +value >= 10000
: absNumericValue >= 10000
? NumericUnit.TenThousand
: NumericUnit.None;
let formattedValue = formatByUnit(value, unit);
formattedValue = formatByDecimalPlaces(
formattedValue,
unit === NumericUnit.OneHundredMillion ? 2 : +value < 1 ? 3 : 1
unit === NumericUnit.OneHundredMillion ? 2 : absNumericValue < 1 ? 3 : 1
);
formattedValue = formatByThousandSeperator(formattedValue);
if ((typeof formattedValue === 'number' && isNaN(formattedValue)) || +formattedValue === 0) {
@@ -93,7 +96,7 @@ export const getFormattedValue = (value: number | string, remainZero?: boolean)
export const formatNumberWithCN = (num: number) => {
if (isNaN(num)) return '-';
if (num >= 10000) {
if (Math.abs(+num) >= 10000) {
return (num / 10000).toFixed(1) + '万';
} else {
return formatByDecimalPlaces(num, 2);

View File

@@ -268,7 +268,7 @@ export function formatByDecimalPlaces(value: number | string, decimalPlaces: num
return value;
}
let str = (+value).toFixed(decimalPlaces);
if (!/^[0-9.]+$/g.test(str)) {
if (!/^-?[0-9.]+$/g.test(str)) {
return '0';
}
while (str.includes('.') && (str.endsWith('.') || str.endsWith('0'))) {
@@ -337,17 +337,20 @@ export const getFormattedValueData = (value: number | string, remainZero?: boole
if (!isFinite(+value)) {
return value;
}
const absNumericValue = Math.abs(+value);
const unit =
value >= 100000000
absNumericValue >= 100000000
? NumericUnit.OneHundredMillion
: value >= 10000
: absNumericValue >= 10000
? NumericUnit.EnTenThousand
: NumericUnit.None;
let formattedValue = formatByUnit(value, unit);
formattedValue = formatByDecimalPlaces(
formattedValue,
unit === NumericUnit.OneHundredMillion ? 2 : value < 1 ? 3 : 1,
unit === NumericUnit.OneHundredMillion ? 2 : absNumericValue < 1 ? 3 : 1,
);
formattedValue = formatByThousandSeperator(formattedValue);
if ((typeof formattedValue === 'number' && isNaN(formattedValue)) || +formattedValue === 0) {