diff --git a/app/presenters/base.presenter.js b/app/presenters/base.presenter.js index 92e89edbe4..11ac87160b 100644 --- a/app/presenters/base.presenter.js +++ b/app/presenters/base.presenter.js @@ -129,6 +129,22 @@ function formatNumberAsMoney (value, includeSymbol = false) { return `${symbol}${value.toFixed(2)}` } +/** + * Formats a number which represents a value in pounds as a money string with commas, for example, 2889 as '2,889.00' + * + * > Credit to https://stackoverflow.com/a/32154217/6117745 + * + * @param {Number} value The value to display as currency. Assumed to be in pounds + * @param {Boolean} includeSymbol Whether to add the £ symbol to the start of the returned string + * + * @returns {string} The value formatted as a money string with commas with optional currency symbol + */ +function formatNumberAsMoneyWithCommas (value, includeSymbol = false) { + const symbol = includeSymbol ? '£' : '' + + return `${symbol}${value.toLocaleString('en-GB', { minimumFractionDigits: 2 })}` +} + /** * Pads a number to a given length with leading zeroes and returns the result as a string * @@ -152,5 +168,6 @@ module.exports = { formatLongDate, formatLongDateTime, formatNumberAsMoney, + formatNumberAsMoneyWithCommas, leftPadZeroes } diff --git a/test/presenters/base.presenter.test.js b/test/presenters/base.presenter.test.js index a665d4b459..74fb20b3dd 100644 --- a/test/presenters/base.presenter.test.js +++ b/test/presenters/base.presenter.test.js @@ -167,11 +167,11 @@ describe('Base presenter', () => { }) describe('#formatNumberAsMoney()', () => { - const valueInPence = 1149.5 + const valueInPounds = 1149.5 describe('when no £ symbol is requested', () => { it('correctly returns the value as a money string with no symbol, for example, 1149.50', async () => { - const result = BasePresenter.formatNumberAsMoney(valueInPence) + const result = BasePresenter.formatNumberAsMoney(valueInPounds) expect(result).to.equal('1149.50') }) @@ -179,13 +179,33 @@ describe('Base presenter', () => { describe('when the £ symbol is requested', () => { it('correctly returns the value as a money string with a symbol, for example, £1149.50', async () => { - const result = BasePresenter.formatNumberAsMoney(valueInPence, true) + const result = BasePresenter.formatNumberAsMoney(valueInPounds, true) expect(result).to.equal('£1149.50') }) }) }) + describe('#formatNumberAsMoneyWithCommas()', () => { + const valueInPounds = 1149.5 + + describe('when no £ symbol is requested', () => { + it('correctly returns the value as a money string with commas and no symbol, for example, 1,149.50', async () => { + const result = BasePresenter.formatNumberAsMoneyWithCommas(valueInPounds) + + expect(result).to.equal('1,149.50') + }) + }) + + describe('when the £ symbol is requested', () => { + it('correctly returns the value as a money string with commas and a symbol, for example, £1,149.50', async () => { + const result = BasePresenter.formatNumberAsMoneyWithCommas(valueInPounds, true) + + expect(result).to.equal('£1,149.50') + }) + }) + }) + describe('#leftPadZeroes()', () => { it('correctly pads numbers', async () => { const number = 123