mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-16 16:07:33 +08:00
code refactor
This commit is contained in:
@@ -109,58 +109,6 @@ export function getObjectOwnFieldCount(object) {
|
||||
return count;
|
||||
}
|
||||
|
||||
export function appendThousandsSeparator(value, enable) {
|
||||
if (!enable || value.length <= 3) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const negative = value.charAt(0) === '-';
|
||||
|
||||
if (negative) {
|
||||
value = value.substring(1);
|
||||
}
|
||||
|
||||
const dotPos = value.indexOf('.');
|
||||
const integer = dotPos < 0 ? value : value.substring(0, dotPos);
|
||||
const decimals = dotPos < 0 ? '' : value.substring(dotPos + 1, value.length);
|
||||
|
||||
const finalChars = [];
|
||||
|
||||
for (let i = 0; i < integer.length; i++) {
|
||||
if (i % 3 === 0 && i > 0) {
|
||||
finalChars.push(',');
|
||||
}
|
||||
|
||||
finalChars.push(integer.charAt(integer.length - 1 - i));
|
||||
}
|
||||
|
||||
finalChars.reverse();
|
||||
|
||||
let newInteger = finalChars.join('');
|
||||
|
||||
if (negative) {
|
||||
newInteger = `-${newInteger}`;
|
||||
}
|
||||
|
||||
if (dotPos < 0) {
|
||||
return newInteger;
|
||||
} else {
|
||||
return `${newInteger}.${decimals}`;
|
||||
}
|
||||
}
|
||||
|
||||
export function formatPercent(value, precision, lowPrecisionValue) {
|
||||
const ratio = Math.pow(10, precision);
|
||||
const normalizedValue = Math.floor(value * ratio);
|
||||
|
||||
if (value > 0 && normalizedValue < 1 && lowPrecisionValue) {
|
||||
return lowPrecisionValue + '%';
|
||||
}
|
||||
|
||||
const result = normalizedValue / ratio;
|
||||
return result + '%';
|
||||
}
|
||||
|
||||
export function limitText(value, maxLength) {
|
||||
let length = 0;
|
||||
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
import { isNumber, appendThousandsSeparator } from './common.js';
|
||||
import { isNumber } from './common.js';
|
||||
import { appendThousandsSeparator } from './numeral.js';
|
||||
|
||||
export function numericCurrencyToString(num, enableThousandsSeparator, trimTailZero) {
|
||||
let str = num.toString();
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
export function appendThousandsSeparator(value, enable) {
|
||||
if (!enable || value.length <= 3) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const negative = value.charAt(0) === '-';
|
||||
|
||||
if (negative) {
|
||||
value = value.substring(1);
|
||||
}
|
||||
|
||||
const dotPos = value.indexOf('.');
|
||||
const integer = dotPos < 0 ? value : value.substring(0, dotPos);
|
||||
const decimals = dotPos < 0 ? '' : value.substring(dotPos + 1, value.length);
|
||||
|
||||
const finalChars = [];
|
||||
|
||||
for (let i = 0; i < integer.length; i++) {
|
||||
if (i % 3 === 0 && i > 0) {
|
||||
finalChars.push(',');
|
||||
}
|
||||
|
||||
finalChars.push(integer.charAt(integer.length - 1 - i));
|
||||
}
|
||||
|
||||
finalChars.reverse();
|
||||
|
||||
let newInteger = finalChars.join('');
|
||||
|
||||
if (negative) {
|
||||
newInteger = `-${newInteger}`;
|
||||
}
|
||||
|
||||
if (dotPos < 0) {
|
||||
return newInteger;
|
||||
} else {
|
||||
return `${newInteger}.${decimals}`;
|
||||
}
|
||||
}
|
||||
|
||||
export function formatPercent(value, precision, lowPrecisionValue) {
|
||||
const ratio = Math.pow(10, precision);
|
||||
const normalizedValue = Math.floor(value * ratio);
|
||||
|
||||
if (value > 0 && normalizedValue < 1 && lowPrecisionValue) {
|
||||
return lowPrecisionValue + '%';
|
||||
}
|
||||
|
||||
const result = normalizedValue / ratio;
|
||||
return result + '%';
|
||||
}
|
||||
Reference in New Issue
Block a user