Skip to content

Commit

Permalink
feat: CP-9655 check the fraction numebr and transform (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava authored Jan 30, 2025
1 parent 8983e9e commit 687f1b7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
43 changes: 43 additions & 0 deletions src/contexts/utils/getCurrencyFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
21 changes: 18 additions & 3 deletions src/contexts/utils/getCurrencyFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <symbol><amount>
Expand All @@ -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;
};

0 comments on commit 687f1b7

Please sign in to comment.