63 lines
2.1 KiB
JavaScript
63 lines
2.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ThousandCont = exports.CentToString = exports.StringToCent = exports.ToYuan = exports.ToCent = void 0;
|
|
const ToCent = (float) => {
|
|
return Math.round(float * 100);
|
|
};
|
|
exports.ToCent = ToCent;
|
|
const ToYuan = (int) => {
|
|
return Math.round(int) / 100;
|
|
};
|
|
exports.ToYuan = ToYuan;
|
|
const StringToCent = (value, allowNegative) => {
|
|
const numValue = parseInt(value, 10);
|
|
if (typeof numValue === 'number' && (numValue >= 0 || allowNegative)) {
|
|
return ToCent(numValue);
|
|
}
|
|
};
|
|
exports.StringToCent = StringToCent;
|
|
const CentToString = (value, fixed) => {
|
|
if (typeof value === 'number') {
|
|
return ToYuan(value).toFixed(fixed);
|
|
}
|
|
};
|
|
exports.CentToString = CentToString;
|
|
const ThousandCont = (value, decimalPlaces) => {
|
|
let valueStr = typeof value === 'number' ? value.toString() : value;
|
|
// 处理负号
|
|
let isNegative = false;
|
|
if (valueStr.startsWith('-')) {
|
|
isNegative = true;
|
|
valueStr = valueStr.slice(1); // 移除负号
|
|
}
|
|
// 分离整数部分和小数部分
|
|
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 (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 {
|
|
return formattedInteger;
|
|
}
|
|
};
|
|
exports.ThousandCont = ThousandCont;
|