ethjs-unit icon indicating copy to clipboard operation
ethjs-unit copied to clipboard

toWei: incorrect result for small numbers (< 1e-6)

Open slavafomin opened this issue 3 years ago • 1 comments
trafficstars

Issue Type

  • [X] Bug (https://github.com/ethjs/ethjs-unit/blob/master/.github/CONTRIBUTING.md#bug-reports)
  • [ ] Feature (https://github.com/ethjs/ethjs-unit/blob/master/.github/CONTRIBUTING.md#feature-requests)

Description

The toWei() function will return incorrect results when passing numbers smaller than 1e-6 to it.

For example toWei(1e-7, 'gwei').toString() will return 2377000000000 instead of 100.

The issue starts here and then cascades downward. The problem is that you are using the String constructor to convert number to a string. However, it will produce some small numbers in exponential notation instead of the fixed notation that you are expecting.

Steps to reproduce

Check the PR https://github.com/ethjs/ethjs-unit/pull/7.

Versions

Doesn't matter.

slavafomin avatar Jun 02 '22 14:06 slavafomin

Check the PR https://github.com/ethjs/ethjs-unit/pull/7 for a fix.

slavafomin avatar Jun 02 '22 15:06 slavafomin