From 62de7c2713c140ef757d821d7538a965ea625b7e Mon Sep 17 00:00:00 2001 From: Ghislain Beaulac Date: Tue, 17 Mar 2020 19:54:32 -0400 Subject: [PATCH] fix(formatters): decimalSeparator & thousandSeparator work tgt - when using both together, 1 was cancelling the other. To fix this, I rewrote the code use text split by the decimal and then recombining them as a new text with necessary thousand & decimal separators --- .../src/services/__tests__/utilities.spec.ts | 21 +++++++++++++++++++ packages/common/src/services/utilities.ts | 20 ++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/common/src/services/__tests__/utilities.spec.ts b/packages/common/src/services/__tests__/utilities.spec.ts index 98b13d81b..215f98a73 100644 --- a/packages/common/src/services/__tests__/utilities.spec.ts +++ b/packages/common/src/services/__tests__/utilities.spec.ts @@ -331,6 +331,17 @@ describe('Service/Utilies', () => { expect(output2).toBe('12,345,678'); }); + it('should return a string without decimals but using dot (.) as thousand separator when these arguments are null or undefined and the input provided is an integer', () => { + const input = 12345678; + const decimalSeparator = ','; + const thousandSeparator = '.'; + const output1 = formatNumber(input, null, null, false, '', '', decimalSeparator, thousandSeparator); + const output2 = formatNumber(input, undefined, undefined, false, '', '', decimalSeparator, thousandSeparator); + + expect(output1).toBe('12.345.678'); + expect(output2).toBe('12.345.678'); + }); + it('should return a formatted string wrapped in parentheses when the input number is negative and the displayNegativeNumberWithParentheses argument is enabled', () => { const input = -123; const displayNegativeNumberWithParentheses = true; @@ -372,6 +383,16 @@ describe('Service/Utilies', () => { expect(output).toBe('-$12,345,678.00'); }); + it('should return a formatted currency string and thousand separator using dot (.) and decimal using comma (,) when those are provided', () => { + const input = -12345678.32; + const displayNegativeNumberWithParentheses = false; + const currencyPrefix = '$'; + const decimalSeparator = ','; + const thousandSeparator = '.'; + const output = formatNumber(input, 2, 2, displayNegativeNumberWithParentheses, currencyPrefix, '', decimalSeparator, thousandSeparator); + expect(output).toBe('-$12.345.678,32'); + }); + it('should return a formatted currency string with symbol prefix/suffix wrapped in parentheses when the input number is negative, when all necessary arguments are filled', () => { const input = -1234; const displayNegativeNumberWithParentheses = true; diff --git a/packages/common/src/services/utilities.ts b/packages/common/src/services/utilities.ts index 4822d1d1d..929f1c2c3 100644 --- a/packages/common/src/services/utilities.ts +++ b/packages/common/src/services/utilities.ts @@ -102,17 +102,29 @@ export function decimalFormatted(input: number | string, minDecimal?: number, ma amount += '0'; } + const decimalSplit = amount.split('.'); + let integerNumber; + let decimalNumber; + // do we want to display our number with a custom separator in each thousand position if (thousandSeparator) { - amount = thousandSeparatorFormatted(amount, thousandSeparator) || ''; + integerNumber = decimalSplit.length >= 1 ? thousandSeparatorFormatted(decimalSplit[0], thousandSeparator) : undefined; + } else { + integerNumber = decimalSplit.length >= 1 ? decimalSplit[0] : amount; } // when using a separator that is not a dot, replace it with the new separator - if (decimalSeparator !== '.') { - amount = amount.replace('.', decimalSeparator); + if (decimalSplit.length > 1) { + decimalNumber = decimalSplit[1]; } - return amount; + let output = ''; + if (integerNumber !== undefined && decimalNumber !== undefined) { + output = `${integerNumber}${decimalSeparator}${decimalNumber}`; + } else { + output = integerNumber; + } + return output; } /**