mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-14 15:07:33 +08:00
86 lines
2.0 KiB
JavaScript
86 lines
2.0 KiB
JavaScript
import { isNumber, appendThousandsSeparator } from './common.js';
|
|
|
|
export function numericCurrencyToString(num, enableThousandsSeparator) {
|
|
let str = num.toString();
|
|
const negative = str.charAt(0) === '-';
|
|
|
|
if (negative) {
|
|
str = str.substring(1);
|
|
}
|
|
|
|
if (str.length === 0) {
|
|
str = '0.00';
|
|
} else if (str.length === 1) {
|
|
str = '0.0' + str;
|
|
} else if (str.length === 2) {
|
|
str = '0.' + str;
|
|
} else {
|
|
let integer = str.substring(0, str.length - 2);
|
|
let decimals = str.substring(str.length - 2);
|
|
|
|
integer = appendThousandsSeparator(integer, enableThousandsSeparator);
|
|
|
|
str = `${integer}.${decimals}`;
|
|
}
|
|
|
|
if (negative) {
|
|
str = `-${str}`;
|
|
}
|
|
|
|
return str;
|
|
}
|
|
|
|
export function stringCurrencyToNumeric(str) {
|
|
if (!str || str.length < 1) {
|
|
return 0;
|
|
}
|
|
|
|
const negative = str.charAt(0) === '-';
|
|
|
|
if (negative) {
|
|
str = str.substring(1);
|
|
}
|
|
|
|
if (!str || str.length < 1) {
|
|
return 0;
|
|
}
|
|
|
|
const sign = negative ? -1 : 1;
|
|
|
|
if (str.indexOf(',')) {
|
|
str = str.replaceAll(/,/g, '');
|
|
}
|
|
|
|
let dotPos = str.indexOf('.');
|
|
|
|
if (dotPos < 0) {
|
|
return sign * parseInt(str) * 100;
|
|
} else if (dotPos === 0) {
|
|
str = '0' + str;
|
|
dotPos++;
|
|
}
|
|
|
|
const integer = str.substring(0, dotPos);
|
|
const decimals = str.substring(dotPos + 1, str.length);
|
|
|
|
if (decimals.length < 1) {
|
|
return sign * parseInt(integer) * 100;
|
|
} else if (decimals.length === 1) {
|
|
return sign * parseInt(integer) * 100 + sign * parseInt(decimals) * 10;
|
|
} else if (decimals.length === 2) {
|
|
return sign * parseInt(integer) * 100 + sign * parseInt(decimals);
|
|
} else {
|
|
return sign * parseInt(integer) * 100 + sign * parseInt(decimals.substring(0, 2));
|
|
}
|
|
}
|
|
|
|
export function getExchangedAmount(amount, fromRate, toRate) {
|
|
const exchangeRate = parseFloat(toRate) / parseFloat(fromRate);
|
|
|
|
if (!isNumber(exchangeRate)) {
|
|
return null;
|
|
}
|
|
|
|
return amount * exchangeRate;
|
|
}
|