fix: ThousandCont函数处理金额负号不对

This commit is contained in:
wkj 2025-12-10 17:57:59 +08:00
parent d354c28a3b
commit 64f855c176
3 changed files with 69 additions and 39 deletions

View File

@ -2,5 +2,5 @@ declare const ToCent: (float: number) => number;
declare const ToYuan: (int: number) => number; declare const ToYuan: (int: number) => number;
declare const StringToCent: (value: string, allowNegative?: true) => number | undefined; declare const StringToCent: (value: string, allowNegative?: true) => number | undefined;
declare const CentToString: (value: number, fixed?: number) => string | undefined; declare const CentToString: (value: number, fixed?: number) => string | undefined;
declare const ThousandCont: (value: number | string, decimalPlaces?: number) => string | undefined; declare const ThousandCont: (value: number | string, decimalPlaces?: number) => string;
export { ToCent, ToYuan, StringToCent, CentToString, ThousandCont }; export { ToCent, ToYuan, StringToCent, CentToString, ThousandCont };

View File

@ -23,29 +23,40 @@ const CentToString = (value, fixed) => {
}; };
exports.CentToString = CentToString; exports.CentToString = CentToString;
const ThousandCont = (value, decimalPlaces) => { const ThousandCont = (value, decimalPlaces) => {
let value1 = typeof value === 'number' ? `${value}` : value; let valueStr = typeof value === 'number' ? value.toString() : value;
const numArr = value1.split('.'); // 处理负号
value1 = numArr[0]; let isNegative = false;
let result = ''; if (valueStr.startsWith('-')) {
while (value1.length > 3) { isNegative = true;
result = ',' + value1.slice(-3) + result; valueStr = valueStr.slice(1); // 移除负号
value1 = value1.slice(0, value1.length - 3);
} }
if (value1) { // 分离整数部分和小数部分
result = value1 + result; const [integerPart, decimalPart = ''] = valueStr.split('.');
// 千分位格式化整数部分
let formattedInteger = '';
let remainingInteger = integerPart;
while (remainingInteger.length > 3) {
formattedInteger = ',' + remainingInteger.slice(-3) + formattedInteger;
remainingInteger = remainingInteger.slice(0, remainingInteger.length - 3);
} }
if (decimalPlaces && decimalPlaces > 0) { if (remainingInteger) {
if (numArr[1]) { formattedInteger = remainingInteger + formattedInteger;
const decimalPart = numArr[1].padEnd(decimalPlaces, '0').slice(0, decimalPlaces); }
result = result + '.' + decimalPart; // 恢复负号
} if (isNegative) {
else { formattedInteger = '-' + formattedInteger;
result = result + '.' + '0'.repeat(decimalPlaces); }
} // 处理小数部分
let formattedDecimal = '';
if (decimalPlaces !== undefined && decimalPlaces > 0) {
formattedDecimal = decimalPart.padEnd(decimalPlaces, '0').slice(0, decimalPlaces);
return formattedInteger + '.' + formattedDecimal;
}
else if (decimalPart) {
return formattedInteger + '.' + decimalPart;
} }
else { else {
result = numArr[1] ? result + '.' + numArr[1] : result; return formattedInteger;
} }
return result;
}; };
exports.ThousandCont = ThousandCont; exports.ThousandCont = ThousandCont;

View File

@ -22,29 +22,48 @@ const CentToString: (value: number, fixed?: number) => string | undefined = (val
} }
}; };
const ThousandCont: (value: number | string, decimalPlaces?: number) => string | undefined = (value, decimalPlaces) => { const ThousandCont: (value: number | string, decimalPlaces?: number) => string = (value, decimalPlaces) => {
let value1 = typeof value === 'number' ? `${value}`: value; let valueStr = typeof value === 'number' ? value.toString() : value;
const numArr = value1.split('.');
value1 = numArr[0]; // 处理负号
let result = ''; let isNegative = false;
while (value1.length > 3) { if (valueStr.startsWith('-')) {
result = ',' + value1.slice(-3) + result; isNegative = true;
value1 = value1.slice(0, value1.length - 3); valueStr = valueStr.slice(1); // 移除负号
} }
if (value1) {
result = value1 + result; // 分离整数部分和小数部分
const [integerPart, decimalPart = ''] = valueStr.split('.');
// 千分位格式化整数部分
let formattedInteger = '';
let remainingInteger = integerPart;
while (remainingInteger.length > 3) {
formattedInteger = ',' + remainingInteger.slice(-3) + formattedInteger;
remainingInteger = remainingInteger.slice(0, remainingInteger.length - 3);
} }
if (decimalPlaces && decimalPlaces > 0) {
if (numArr[1]) { if (remainingInteger) {
const decimalPart = numArr[1].padEnd(decimalPlaces, '0').slice(0, decimalPlaces); formattedInteger = remainingInteger + formattedInteger;
result = result + '.' + decimalPart; }
} else {
result = result + '.' + '0'.repeat(decimalPlaces); // 恢复负号
} if (isNegative) {
formattedInteger = '-' + formattedInteger;
}
// 处理小数部分
let formattedDecimal = '';
if (decimalPlaces !== undefined && decimalPlaces > 0) {
formattedDecimal = decimalPart.padEnd(decimalPlaces, '0').slice(0, decimalPlaces);
return formattedInteger + '.' + formattedDecimal;
} else if (decimalPart) {
return formattedInteger + '.' + decimalPart;
} else { } else {
result = numArr[1] ? result + '.' + numArr[1] : result; return formattedInteger;
} }
return result;
}; };
export { ToCent, ToYuan, StringToCent, CentToString, ThousandCont }; export { ToCent, ToYuan, StringToCent, CentToString, ThousandCont };