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

Commit 20a9984

Browse files
authored
Merge pull request #463 from lightninglabs/fix-toamount
Fix bug where number smaller than 100 SAT would displayed in exponent…
2 parents 153d5af + d7d6cd5 commit 20a9984

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/helper.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export const toAmount = (satoshis, unit) => {
8282
if (!Number.isInteger(satoshis) || !UNITS[unit]) {
8383
throw new Error('Invalid input!');
8484
}
85-
return (satoshis / UNITS[unit].denominator).toString();
85+
const num = satoshis / UNITS[unit].denominator;
86+
return num.toLocaleString('en-US', {
87+
useGrouping: false,
88+
maximumFractionDigits: 8,
89+
});
8690
};
8791

8892
/**

test/unit/helper.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,26 @@ describe('Helpers Unit Tests', () => {
275275
const num = helpers.toAmount(0, 'btc');
276276
expect(num, 'to equal', '0');
277277
});
278+
279+
it('should work for 1', () => {
280+
const num = helpers.toAmount(1, 'btc');
281+
expect(num, 'to equal', '0.00000001');
282+
});
283+
284+
it('should work for 10', () => {
285+
const num = helpers.toAmount(10, 'btc');
286+
expect(num, 'to equal', '0.0000001');
287+
});
288+
289+
it('should work for 100', () => {
290+
const num = helpers.toAmount(100, 'btc');
291+
expect(num, 'to equal', '0.000001');
292+
});
293+
294+
it('should work for 1000', () => {
295+
const num = helpers.toAmount(1000, 'btc');
296+
expect(num, 'to equal', '0.00001');
297+
});
278298
});
279299

280300
describe('calculateExchangeRate()', () => {

0 commit comments

Comments
 (0)