diff --git a/.gitignore b/.gitignore index 3260b1f..c9ffb7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Don't check auto-generated stuff into git +.idea node_modules coverage lib diff --git a/src/index.js b/src/index.js index 55b62fe..c4f3d90 100644 --- a/src/index.js +++ b/src/index.js @@ -69,7 +69,15 @@ function numberToString(arg) { } return arg; } else if (typeof arg === 'number') { - return String(arg); + if (arg === -0) { + return '0'; + } + // Using Intl to reliably convert number to string, + // toString/toFixed will give incorrect results for some numbers + return arg.toLocaleString('en', { + maximumFractionDigits: 20, + useGrouping: false, + }); } else if (typeof arg === 'object' && arg.toString && (arg.toTwos || arg.dividedToIntegerBy)) { if (arg.toPrecision) { return String(arg.toPrecision()); diff --git a/src/tests/test.index.js b/src/tests/test.index.js index 0397aa1..89ca66e 100644 --- a/src/tests/test.index.js +++ b/src/tests/test.index.js @@ -74,6 +74,12 @@ describe('toWei', () => { assert.equal(units.toWei(1, 'milli').toString(10), units.toWei(1000, 'micro').toString(10)); assert.throws(() => { units.toWei(1, 'wei1'); }, Error); + + assert.equal(units.toWei(0.1, 'gwei').toString(10), '100000000'); + }); + + it('should return the correct value for small numbers', () => { + assert.equal(units.toWei(1e-7, 'gwei').toString(10), '100'); }); });