oak-domain/lib/utils/money.js

52 lines
1.6 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 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);
}
if (value1) {
result = value1 + result;
}
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);
}
}
else {
result = numArr[1] ? result + '.' + numArr[1] : result;
}
return result;
};
exports.ThousandCont = ThousandCont;