Skip to content

Commit 687f1b7

Browse files
authored
feat: CP-9655 check the fraction numebr and transform (#128)
1 parent 8983e9e commit 687f1b7

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { getCurrencyFormatter } from './getCurrencyFormatter';
2+
3+
describe('contexts/utils/getCurrencyFormatter', () => {
4+
const formatter = getCurrencyFormatter();
5+
it('should return with the dollar sign and two fraction numbers', () => {
6+
const value = formatter(12);
7+
expect(value).toBe('$12.00');
8+
9+
const value2 = formatter(12.1);
10+
expect(value2).toBe('$12.10');
11+
12+
const value3 = formatter(12.01);
13+
expect(value3).toBe('$12.01');
14+
15+
const value4 = formatter(12.011);
16+
expect(value4).toBe('$12.01');
17+
});
18+
it('should return with the dollar sign and the first two fraction numbers when the integer is a zero', () => {
19+
const value = formatter(0.123);
20+
expect(value).toBe('$0.12');
21+
22+
const value2 = formatter(0.02);
23+
expect(value2).toBe('$0.02');
24+
25+
const value3 = formatter(0.0234);
26+
expect(value3).toBe('$0.02');
27+
});
28+
it('should return with the dollar sign and look for the first non-zero fraction number', () => {
29+
const value = formatter(0.00023);
30+
expect(value).toBe('$0.0002');
31+
32+
const value2 = formatter(0.000023);
33+
expect(value2).toBe('$0.00002');
34+
35+
// 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
36+
const value3 = formatter(0.0000023);
37+
expect(value3).toBe('$0.000002');
38+
39+
// this is with scientific notation
40+
const value4 = formatter(0.00000023);
41+
expect(value4).not.toBe('$0.0000002');
42+
});
43+
});

src/contexts/utils/getCurrencyFormatter.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ export const getCurrencyFormatter = (currency = 'USD') => {
66
style: 'currency',
77
currency: currency,
88
currencyDisplay: 'narrowSymbol',
9-
maximumSignificantDigits: 6,
9+
maximumFractionDigits: 6,
1010
});
1111

1212
return (amount: number) => {
13-
const parts = formatter.formatToParts(amount);
13+
const transformedAmount = modifyFractionNumber(amount);
14+
const parts = formatter.formatToParts(transformedAmount);
15+
1416
/**
1517
* This formats the currency to return
1618
* <symbol><amount>
@@ -26,6 +28,19 @@ export const getCurrencyFormatter = (currency = 'USD') => {
2628
return flatArray.join('').trim();
2729
}
2830

29-
return formatter.format(amount);
31+
return formatter.format(transformedAmount);
3032
};
3133
};
34+
35+
const modifyFractionNumber = (amount: number) => {
36+
const [integer, fraction] = amount.toString().split('.');
37+
const indexOfNonZero = fraction?.search(/[1-9]/);
38+
39+
if (!indexOfNonZero || indexOfNonZero < 2 || integer !== '0') {
40+
return parseFloat(`${integer}.${fraction?.slice(0, 2)}`);
41+
}
42+
if (indexOfNonZero && indexOfNonZero >= 2 && integer === '0') {
43+
return parseFloat(`${integer}.${fraction?.slice(0, indexOfNonZero + 1)}`);
44+
}
45+
return amount;
46+
};

0 commit comments

Comments
 (0)