From 687f1b78b1f1ad26cd565a6de8bbe653dc0c8774 Mon Sep 17 00:00:00 2001 From: vvava <94397450+vvava@users.noreply.github.com> Date: Thu, 30 Jan 2025 15:24:03 +0100 Subject: [PATCH] feat: CP-9655 check the fraction numebr and transform (#128) --- .../utils/getCurrencyFormatter.test.ts | 43 +++++++++++++++++++ src/contexts/utils/getCurrencyFormatter.ts | 21 +++++++-- 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/contexts/utils/getCurrencyFormatter.test.ts diff --git a/src/contexts/utils/getCurrencyFormatter.test.ts b/src/contexts/utils/getCurrencyFormatter.test.ts new file mode 100644 index 00000000..118e54eb --- /dev/null +++ b/src/contexts/utils/getCurrencyFormatter.test.ts @@ -0,0 +1,43 @@ +import { getCurrencyFormatter } from './getCurrencyFormatter'; + +describe('contexts/utils/getCurrencyFormatter', () => { + const formatter = getCurrencyFormatter(); + it('should return with the dollar sign and two fraction numbers', () => { + const value = formatter(12); + expect(value).toBe('$12.00'); + + const value2 = formatter(12.1); + expect(value2).toBe('$12.10'); + + const value3 = formatter(12.01); + expect(value3).toBe('$12.01'); + + const value4 = formatter(12.011); + expect(value4).toBe('$12.01'); + }); + it('should return with the dollar sign and the first two fraction numbers when the integer is a zero', () => { + const value = formatter(0.123); + expect(value).toBe('$0.12'); + + const value2 = formatter(0.02); + expect(value2).toBe('$0.02'); + + const value3 = formatter(0.0234); + expect(value3).toBe('$0.02'); + }); + it('should return with the dollar sign and look for the first non-zero fraction number', () => { + const value = formatter(0.00023); + expect(value).toBe('$0.0002'); + + const value2 = formatter(0.000023); + expect(value2).toBe('$0.00002'); + + // NOTE: this is the last point (in terms of the number of the fraction part) we handle, below that the scientific notation is not something we deal with at the moment + const value3 = formatter(0.0000023); + expect(value3).toBe('$0.000002'); + + // this is with scientific notation + const value4 = formatter(0.00000023); + expect(value4).not.toBe('$0.0000002'); + }); +}); diff --git a/src/contexts/utils/getCurrencyFormatter.ts b/src/contexts/utils/getCurrencyFormatter.ts index 510497fc..c581a8db 100644 --- a/src/contexts/utils/getCurrencyFormatter.ts +++ b/src/contexts/utils/getCurrencyFormatter.ts @@ -6,11 +6,13 @@ export const getCurrencyFormatter = (currency = 'USD') => { style: 'currency', currency: currency, currencyDisplay: 'narrowSymbol', - maximumSignificantDigits: 6, + maximumFractionDigits: 6, }); return (amount: number) => { - const parts = formatter.formatToParts(amount); + const transformedAmount = modifyFractionNumber(amount); + const parts = formatter.formatToParts(transformedAmount); + /** * This formats the currency to return * @@ -26,6 +28,19 @@ export const getCurrencyFormatter = (currency = 'USD') => { return flatArray.join('').trim(); } - return formatter.format(amount); + return formatter.format(transformedAmount); }; }; + +const modifyFractionNumber = (amount: number) => { + const [integer, fraction] = amount.toString().split('.'); + const indexOfNonZero = fraction?.search(/[1-9]/); + + if (!indexOfNonZero || indexOfNonZero < 2 || integer !== '0') { + return parseFloat(`${integer}.${fraction?.slice(0, 2)}`); + } + if (indexOfNonZero && indexOfNonZero >= 2 && integer === '0') { + return parseFloat(`${integer}.${fraction?.slice(0, indexOfNonZero + 1)}`); + } + return amount; +};