add thousands separator

This commit is contained in:
MaysWind
2020-11-10 00:46:58 +08:00
parent 66c71344da
commit a204b647be
5 changed files with 40 additions and 3 deletions
+23 -3
View File
@@ -1,7 +1,22 @@
import settings from "../lib/settings.js";
import utils from "../lib/utils.js";
export default function ({ i18n }, value, currencyCode) {
function appendThousandsSeparator(value) {
const finalChars = [];
for (let i = 0; i < value.length; i++) {
if (i % 3 === 0 && i > 0) {
finalChars.push(',');
}
finalChars.push(value.charAt(value.length - 1 - i));
}
finalChars.reverse();
return finalChars.join('');
}
export default function ({i18n}, value, currencyCode) {
if (!utils.isNumber(value) && !utils.isString(value)) {
return value;
}
@@ -17,8 +32,13 @@ export default function ({ i18n }, value, currencyCode) {
} else if (value.length === 2) {
value = '0.' + value;
} else {
const integer = value.substr(0, value.length - 2);
const decimals = value.substr(value.length - 2, 2);
let integer = value.substr(0, value.length - 2);
let decimals = value.substr(value.length - 2, 2);
if (settings.isEnableThousandsSeparator() && integer.length > 3) {
integer = appendThousandsSeparator(integer);
}
value = `${integer}.${decimals}`;
}