Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Don't check auto-generated stuff into git
.idea
node_modules
coverage
lib
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 6 additions & 0 deletions src/tests/test.index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down