From d7d6cd51914963902c583f20743009345f077bbb Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Wed, 18 Jul 2018 18:35:43 +0200 Subject: [PATCH] Fix bug where number smaller than 100 SAT would displayed in exponent format --- src/helper.js | 6 +++++- test/unit/helper.spec.js | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/helper.js b/src/helper.js index 49d1c470a..39a24c4fb 100644 --- a/src/helper.js +++ b/src/helper.js @@ -82,7 +82,11 @@ export const toAmount = (satoshis, unit) => { if (!Number.isInteger(satoshis) || !UNITS[unit]) { throw new Error('Invalid input!'); } - return (satoshis / UNITS[unit].denominator).toString(); + const num = satoshis / UNITS[unit].denominator; + return num.toLocaleString('en-US', { + useGrouping: false, + maximumFractionDigits: 8, + }); }; /** diff --git a/test/unit/helper.spec.js b/test/unit/helper.spec.js index 8ad6c81ca..84ab60c57 100644 --- a/test/unit/helper.spec.js +++ b/test/unit/helper.spec.js @@ -275,6 +275,26 @@ describe('Helpers Unit Tests', () => { const num = helpers.toAmount(0, 'btc'); expect(num, 'to equal', '0'); }); + + it('should work for 1', () => { + const num = helpers.toAmount(1, 'btc'); + expect(num, 'to equal', '0.00000001'); + }); + + it('should work for 10', () => { + const num = helpers.toAmount(10, 'btc'); + expect(num, 'to equal', '0.0000001'); + }); + + it('should work for 100', () => { + const num = helpers.toAmount(100, 'btc'); + expect(num, 'to equal', '0.000001'); + }); + + it('should work for 1000', () => { + const num = helpers.toAmount(1000, 'btc'); + expect(num, 'to equal', '0.00001'); + }); }); describe('calculateExchangeRate()', () => {