Skip to content

Commit

Permalink
fix: Move minus symbol in front of the currency symbol (#522)
Browse files Browse the repository at this point in the history
* fix: Move minus symbol in front of the currency symbol

The minus symbol is put in front of the amount and currency symbol.
Before this change negative GBP and USD amounts were rendered
as £-1.71 and $-5.00 (basically every currency that appears before
the number amount) and after this change they are
rendered as -£1.71 and -$5.00.

Some examples using the same approach to negative currency amounts:

https://docs.microsoft.com/en-us/globalization/locale/currency-formatting
https://english.stackexchange.com/a/124804

* Match string interpolation style

* Make negative currency test case more intuitive and readable
  • Loading branch information
robbdimitrov authored and connor-baer committed Jan 20, 2020
1 parent 598f963 commit 4938795
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/util/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,18 @@ export function formatCurrency(amount, currency, locale) {
get(currency)
)(CURRENCY_SYMBOLS);
const currencyFormat = getCurrencyFormat(currency, locale);
const formattedAmount = toCurrencyNumberFormat(amount, currencyFormat);

const absAmount = Math.abs(amount);
const sign = amount < 0 ? '-' : '';

const formattedAmount = toCurrencyNumberFormat(absAmount, currencyFormat);
const currencyString = addSymbol(
formattedAmount,
currencySymbol,
currencyFormat
);

return currencyString;
return `${sign}${currencyString}`;
}

function toCurrencyNumberFormat(number, currencyFormat) {
Expand Down
15 changes: 15 additions & 0 deletions src/util/currency.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ describe('currency', () => {
testCurrency(inputs, ccy, 'de-DE', outputs);
});
});

describe('handling negative amounts', () => {
it('should place a minus sign in front of the currency', () => {
const negativeInputs = [-11.23, '-1000', -0.98];

const eurOutputs = ['-11,23\xA0€', '-1.000,00\xA0€', '-0,98\xA0€'];
testCurrency(negativeInputs, 'EUR', 'de-DE', eurOutputs);

const usdOutputs = ['-$11.23', '-$1,000.00', '-$0.98'];
testCurrency(negativeInputs, 'USD', 'en-US', usdOutputs);

const gbpOutputs = ['-£11.23', '-£1,000.00', '-£0.98'];
testCurrency(negativeInputs, 'GBP', 'en-GB', gbpOutputs);
});
});
});

describe('formatAmountForLocale()', () => {
Expand Down

1 comment on commit 4938795

@vercel
Copy link

@vercel vercel bot commented on 4938795 Jan 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.