Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const expectedNamedExports = [
'formatAmount',
'formatAmountFromString',
'formatPriceUnit',
'omitTrailingDecimalZeros',
'parseDecimalValue',
'toDinero',
'toDineroFromInteger',
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
formatAmount,
formatAmountFromString,
formatPriceUnit,
omitTrailingDecimalZeros,
parseDecimalValue,
toIntegerAmount,
addSeparatorToDineroString,
Expand Down
21 changes: 21 additions & 0 deletions src/money/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
formatAmount,
formatAmountFromString,
formatPriceUnit,
omitTrailingDecimalZeros,
parseDecimalValue,
toIntegerAmount,
unitDisplayLabels,
Expand Down Expand Up @@ -294,6 +295,26 @@ describe('formatPriceUnit', () => {
);
});

describe('omitTrailingDecimalZeros', () => {
it.each`
price | expected
${'10.00 €'} | ${'10 €'}
${'10,00 €'} | ${'10 €'}
${'1.000,00 €'} | ${'1.000 €'}
${'10.50 €'} | ${'10.50 €'}
${'10,50 €'} | ${'10,50 €'}
${'€10.00'} | ${'€10'}
${'€10.00/Stück'} | ${'€10/Stück'}
${'€10,00/Stück'} | ${'€10/Stück'}
${'10.00 € / Monat'} | ${'10 € / Monat'}
${'10,00 € / Monat'} | ${'10 € / Monat'}
${'0.00 €'} | ${'0 €'}
${'10.20 €'} | ${'10.20 €'}
`('should transform "$price" into "$expected"', ({ price, expected }) => {
expect(omitTrailingDecimalZeros(price)).toEqual(expected);
});
});

describe('parseDecimalValue', () => {
it.each`
value | expected
Expand Down
31 changes: 23 additions & 8 deletions src/money/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,9 @@ export const formatAmountFromString = ({
subunitFromAmount,
);

return formatWithSubunit(
dineroObjectFromAmount
.multiply(100)
.convertPrecision(precision ?? amountPrecision)
.setLocale(locale || DEFAULT_LOCALE)
.toFormat(format || amountFormat),
subunit,
);
const dSubunit = dineroObjectFromAmount.multiply(100).convertPrecision(precision ?? amountPrecision);

return formatWithSubunit(dSubunit.setLocale(locale || DEFAULT_LOCALE).toFormat(format || amountFormat), subunit);
}

return dineroObjectFromAmount
Expand Down Expand Up @@ -352,6 +347,26 @@ function shouldDisplayAmountAsCents(amount: number, currency?: Currency) {
return dAbsoluteAmount.hasSubUnits() && dAbsoluteAmount.lessThan(dAmountOfOneUnit);
}

/**
* Removes trailing decimal zeros (.00 or ,00) from a formatted price string.
* Handles prices with currency symbols, billing period suffixes, and tiered pricing unit suffixes (e.g. €10.00/Stück).
*
* @param price - The formatted price string
* @returns The price string without trailing decimal zeros
*/
export const omitTrailingDecimalZeros = (price: string): string => {
const trailingZerosWithDot = /(\.00)(\s.*)?$/;
const trailingZerosWithComma = /(,00)(\s.*)?$/;
const trailingZerosWithDotBeforeSlash = /(\.00)(\/[\w\W]*)$/;
const trailingZerosWithCommaBeforeSlash = /(,00)(\/[\w\W]*)$/;

return price
.replace(trailingZerosWithDot, '$2')
.replace(trailingZerosWithComma, '$2')
.replace(trailingZerosWithDotBeforeSlash, '$2')
.replace(trailingZerosWithCommaBeforeSlash, '$2');
};

/**
* Converts a decimal string value into a valid decimal amount value, without any thousand separators, using dot as the decimal separator.
*
Expand Down
Loading