display amounts according to currency decimals number count

This commit is contained in:
MaysWind
2024-12-06 23:55:19 +08:00
parent e2f2b325a6
commit e549779164
15 changed files with 313 additions and 23 deletions
+27
View File
@@ -146,6 +146,11 @@ export function formatAmount(value, options) {
}
const decimalSeparator = options.decimalSeparator || numeralConstants.defaultDecimalSeparator.symbol;
let decimalNumberCount = options.decimalNumberCount;
if (!isNumber(decimalNumberCount) || decimalNumberCount > numeralConstants.maxSupportedDecimalNumberCount) {
decimalNumberCount = numeralConstants.defaultDecimalNumberCount;
}
let integer = '0';
let decimals = '00';
@@ -159,6 +164,18 @@ export function formatAmount(value, options) {
decimals = '0' + value;
}
if (decimalNumberCount === 0) {
if (decimals === '00') {
decimals = '';
} else if (decimals.charAt(1) === '0') {
decimals = decimals.charAt(0);
}
} else if (decimalNumberCount === 1) {
if (decimals.charAt(1) === '0') {
decimals = decimals.charAt(0);
}
}
if (options.trimTailZero) {
if (decimals.charAt(0) === '0' && decimals.charAt(1) === '0') {
decimals = '';
@@ -194,6 +211,16 @@ export function formatPercent(value, precision, lowPrecisionValue) {
return result + '%';
}
export function getAmountWithDecimalNumberCount(amount, decimalNumberCount) {
if (decimalNumberCount === 0) {
return Math.floor(amount / 100) * 100;
} else if (decimalNumberCount === 1) {
return Math.floor(amount / 10) * 10;
}
return amount;
}
export function formatExchangeRateAmount(exchangeRateAmount, options) {
if (!options) {
options = {};