Merge branch 'dev' of gitea.51mars.com:Oak-Team/oak-domain into dev

This commit is contained in:
Xu Chang 2025-12-10 18:02:32 +08:00
commit bd055fe6e5
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 StringToCent: (value: string, allowNegative?: true) => number | 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 };

View File

@ -23,29 +23,40 @@ const CentToString = (value, fixed) => {
};
exports.CentToString = CentToString;
const ThousandCont = (value, decimalPlaces) => {
let value1 = typeof value === 'number' ? `${value}` : value;
const numArr = value1.split('.');
value1 = numArr[0];
let result = '';
while (value1.length > 3) {
result = ',' + value1.slice(-3) + result;
value1 = value1.slice(0, value1.length - 3);
let valueStr = typeof value === 'number' ? value.toString() : value;
// 处理负号
let isNegative = false;
if (valueStr.startsWith('-')) {
isNegative = true;
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]) {
const decimalPart = numArr[1].padEnd(decimalPlaces, '0').slice(0, decimalPlaces);
result = result + '.' + decimalPart;
}
else {
result = result + '.' + '0'.repeat(decimalPlaces);
}
if (remainingInteger) {
formattedInteger = remainingInteger + formattedInteger;
}
// 恢复负号
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 {
result = numArr[1] ? result + '.' + numArr[1] : result;
return formattedInteger;
}
return result;
};
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) => {
let value1 = typeof value === 'number' ? `${value}`: value;
const numArr = value1.split('.');
value1 = numArr[0];
let result = '';
while (value1.length > 3) {
result = ',' + value1.slice(-3) + result;
value1 = value1.slice(0, value1.length - 3);
const ThousandCont: (value: number | string, decimalPlaces?: number) => string = (value, decimalPlaces) => {
let valueStr = typeof value === 'number' ? value.toString() : value;
// 处理负号
let isNegative = false;
if (valueStr.startsWith('-')) {
isNegative = true;
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]) {
const decimalPart = numArr[1].padEnd(decimalPlaces, '0').slice(0, decimalPlaces);
result = result + '.' + decimalPart;
} else {
result = result + '.' + '0'.repeat(decimalPlaces);
}
if (remainingInteger) {
formattedInteger = remainingInteger + formattedInteger;
}
// 恢复负号
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 {
result = numArr[1] ? result + '.' + numArr[1] : result;
return formattedInteger;
}
return result;
};
export { ToCent, ToYuan, StringToCent, CentToString, ThousandCont };