Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 704d7c5

Browse files
authored
Merge pull request #1049 from lightninglabs/less-than-cent
Display less than a cent fiat amounts
2 parents 5987b74 + 34a065e commit 704d7c5

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/helper.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ export const formatFiat = (val, currency) => {
3030
num = 0;
3131
}
3232
const options = { style: 'currency', currency };
33-
return new Intl.NumberFormat(undefined, options).format(num);
33+
const intl = new Intl.NumberFormat(undefined, options);
34+
const str = intl.format(num);
35+
if (num < 1 && num > 0 && /0[,.]0{2}/.test(str)) {
36+
return `< ${intl.format(0.01)}`;
37+
} else if (Math.abs(num) < 1 && Math.abs(num) > 0 && /0[,.]0{2}/.test(str)) {
38+
return `< ${intl.format(-0.01)}`;
39+
}
40+
return str;
3441
};
3542

3643
/**

test/unit/helper.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ describe('Helpers Unit Tests', () => {
6060
expect(num, 'to match', /1[,.]0{3}[,.]0{3}[,.]0{2}/);
6161
expect(num, 'to match', /\${1}/);
6262
});
63+
64+
it('should display one cent', () => {
65+
const num = helpers.formatFiat(0.01, 'usd');
66+
expect(num, 'to match', /0[,.]0{1}1{1}/);
67+
expect(num, 'to match', /\${1}/);
68+
expect(num, 'not to match', /^< /);
69+
});
70+
71+
it('should round up to one cent', () => {
72+
const num = helpers.formatFiat(0.005, 'usd');
73+
expect(num, 'to match', /0[,.]0{1}1{1}/);
74+
expect(num, 'to match', /\${1}/);
75+
expect(num, 'not to match', /^< /);
76+
});
77+
78+
it('should display less than a cent', () => {
79+
const num = helpers.formatFiat(0.004, 'usd');
80+
expect(num, 'to match', /0[,.]0{1}1{1}/);
81+
expect(num, 'to match', /\${1}/);
82+
expect(num, 'to match', /^< /);
83+
});
84+
85+
it('should display less than a cent for small numbers', () => {
86+
const num = helpers.formatFiat(0.0000001, 'usd');
87+
expect(num, 'to match', /0[,.]0{1}1{1}/);
88+
expect(num, 'to match', /\${1}/);
89+
expect(num, 'to match', /^< /);
90+
});
91+
92+
it('should display less than a cent for negative small numbers', () => {
93+
const num = helpers.formatFiat(0.999999 - 1, 'usd');
94+
expect(num, 'to match', /0[,.]0{1}1{1}/);
95+
expect(num, 'to match', /\${1}/);
96+
expect(num, 'to match', /^< -/);
97+
});
98+
99+
it('should display 0.00 without less than', () => {
100+
const num = helpers.formatFiat(0.0, 'usd');
101+
expect(num, 'to match', /0[,.]0{2}/);
102+
expect(num, 'to match', /\${1}/);
103+
expect(num, 'not to match', /^< /);
104+
});
63105
});
64106

65107
describe('parseDate()', () => {

0 commit comments

Comments
 (0)