mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 19:51:00 +00:00
(bug)(webapp) support negative number (#2284)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user