mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-12 12:37:55 +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;
|
return value;
|
||||||
}
|
}
|
||||||
let strValue = (+value).toFixed(decimalPlaces);
|
let strValue = (+value).toFixed(decimalPlaces);
|
||||||
if (!/^[0-9.]+$/g.test(strValue)) {
|
if (!/^-?[0-9.]+$/g.test(strValue)) {
|
||||||
return '0';
|
return '0';
|
||||||
}
|
}
|
||||||
while (strValue.includes('.') && (strValue.endsWith('.') || strValue.endsWith('0'))) {
|
while (strValue.includes('.') && (strValue.endsWith('.') || strValue.endsWith('0'))) {
|
||||||
@@ -72,17 +72,20 @@ export const getFormattedValue = (value: number | string, remainZero?: boolean)
|
|||||||
if (!isFinite(+value)) {
|
if (!isFinite(+value)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const absNumericValue = Math.abs(+value);
|
||||||
|
|
||||||
const unit =
|
const unit =
|
||||||
+value >= 100000000
|
absNumericValue >= 100000000
|
||||||
? NumericUnit.OneHundredMillion
|
? NumericUnit.OneHundredMillion
|
||||||
: +value >= 10000
|
: absNumericValue >= 10000
|
||||||
? NumericUnit.TenThousand
|
? NumericUnit.TenThousand
|
||||||
: NumericUnit.None;
|
: NumericUnit.None;
|
||||||
|
|
||||||
let formattedValue = formatByUnit(value, unit);
|
let formattedValue = formatByUnit(value, unit);
|
||||||
formattedValue = formatByDecimalPlaces(
|
formattedValue = formatByDecimalPlaces(
|
||||||
formattedValue,
|
formattedValue,
|
||||||
unit === NumericUnit.OneHundredMillion ? 2 : +value < 1 ? 3 : 1
|
unit === NumericUnit.OneHundredMillion ? 2 : absNumericValue < 1 ? 3 : 1
|
||||||
);
|
);
|
||||||
formattedValue = formatByThousandSeperator(formattedValue);
|
formattedValue = formatByThousandSeperator(formattedValue);
|
||||||
if ((typeof formattedValue === 'number' && isNaN(formattedValue)) || +formattedValue === 0) {
|
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) => {
|
export const formatNumberWithCN = (num: number) => {
|
||||||
if (isNaN(num)) return '-';
|
if (isNaN(num)) return '-';
|
||||||
if (num >= 10000) {
|
if (Math.abs(+num) >= 10000) {
|
||||||
return (num / 10000).toFixed(1) + '万';
|
return (num / 10000).toFixed(1) + '万';
|
||||||
} else {
|
} else {
|
||||||
return formatByDecimalPlaces(num, 2);
|
return formatByDecimalPlaces(num, 2);
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ export function formatByDecimalPlaces(value: number | string, decimalPlaces: num
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
let str = (+value).toFixed(decimalPlaces);
|
let str = (+value).toFixed(decimalPlaces);
|
||||||
if (!/^[0-9.]+$/g.test(str)) {
|
if (!/^-?[0-9.]+$/g.test(str)) {
|
||||||
return '0';
|
return '0';
|
||||||
}
|
}
|
||||||
while (str.includes('.') && (str.endsWith('.') || str.endsWith('0'))) {
|
while (str.includes('.') && (str.endsWith('.') || str.endsWith('0'))) {
|
||||||
@@ -337,17 +337,20 @@ export const getFormattedValueData = (value: number | string, remainZero?: boole
|
|||||||
if (!isFinite(+value)) {
|
if (!isFinite(+value)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const absNumericValue = Math.abs(+value);
|
||||||
|
|
||||||
const unit =
|
const unit =
|
||||||
value >= 100000000
|
absNumericValue >= 100000000
|
||||||
? NumericUnit.OneHundredMillion
|
? NumericUnit.OneHundredMillion
|
||||||
: value >= 10000
|
: absNumericValue >= 10000
|
||||||
? NumericUnit.EnTenThousand
|
? NumericUnit.EnTenThousand
|
||||||
: NumericUnit.None;
|
: NumericUnit.None;
|
||||||
|
|
||||||
let formattedValue = formatByUnit(value, unit);
|
let formattedValue = formatByUnit(value, unit);
|
||||||
formattedValue = formatByDecimalPlaces(
|
formattedValue = formatByDecimalPlaces(
|
||||||
formattedValue,
|
formattedValue,
|
||||||
unit === NumericUnit.OneHundredMillion ? 2 : value < 1 ? 3 : 1,
|
unit === NumericUnit.OneHundredMillion ? 2 : absNumericValue < 1 ? 3 : 1,
|
||||||
);
|
);
|
||||||
formattedValue = formatByThousandSeperator(formattedValue);
|
formattedValue = formatByThousandSeperator(formattedValue);
|
||||||
if ((typeof formattedValue === 'number' && isNaN(formattedValue)) || +formattedValue === 0) {
|
if ((typeof formattedValue === 'number' && isNaN(formattedValue)) || +formattedValue === 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user