diff options
author | Dan Finlay <dan@danfinlay.com> | 2016-04-15 04:20:19 +0800 |
---|---|---|
committer | Dan Finlay <dan@danfinlay.com> | 2016-04-15 04:20:19 +0800 |
commit | 8527d051b0a02bbf9d5f26f10299daf10d647019 (patch) | |
tree | fbcd559a90e6e5e1fb580790ecf78d5bdacc98d6 | |
parent | 5694a116725dad3d3bb9878c9c7059f1870d2e29 (diff) | |
download | tangerine-wallet-browser-8527d051b0a02bbf9d5f26f10299daf10d647019.tar tangerine-wallet-browser-8527d051b0a02bbf9d5f26f10299daf10d647019.tar.gz tangerine-wallet-browser-8527d051b0a02bbf9d5f26f10299daf10d647019.tar.bz2 tangerine-wallet-browser-8527d051b0a02bbf9d5f26f10299daf10d647019.tar.lz tangerine-wallet-browser-8527d051b0a02bbf9d5f26f10299daf10d647019.tar.xz tangerine-wallet-browser-8527d051b0a02bbf9d5f26f10299daf10d647019.tar.zst tangerine-wallet-browser-8527d051b0a02bbf9d5f26f10299daf10d647019.zip |
Fix eth resolution
utils.formatBalance was returning rounded ether, was not useful for displaying account balances.
Now returns four decimal points, and is easily configurable for more, with passing tests.
Spoiler alert: Don't you dare divide bignumber wei. Bignumber does not have decimals. Keep it as wei, format it as ether.
-rw-r--r-- | ui/app/util.js | 9 | ||||
-rw-r--r-- | ui/test/unit/util_test.js | 16 |
2 files changed, 21 insertions, 4 deletions
diff --git a/ui/app/util.js b/ui/app/util.js index 4c31e54f4..74e2c19aa 100644 --- a/ui/app/util.js +++ b/ui/app/util.js @@ -64,11 +64,16 @@ function weiToEth(bn) { return eth } +var decimalsToKeep = 4 function formatBalance(balance) { if (!balance) return 'None' var wei = numericBalance(balance) - var eth = weiToEth(wei) - return eth.toString(10) + ' ETH' + var padded = wei.toString(10) + var len = padded.length + var nonZeroIndex = padded.match(/[^0]/).index + var beforeDecimal = padded.substr(nonZeroIndex ? nonZeroIndex : 0, len - 18) + var afterDecimal = padded.substr(len - 18, decimalsToKeep) + return `${beforeDecimal}.${afterDecimal} ETH` } function dataSize(data) { diff --git a/ui/test/unit/util_test.js b/ui/test/unit/util_test.js index 52635eb89..7e34bba1c 100644 --- a/ui/test/unit/util_test.js +++ b/ui/test/unit/util_test.js @@ -63,9 +63,21 @@ describe('util', function() { }) it('should return eth as string followed by ETH', function() { - var input = new ethUtil.BN(ethInWei).toJSON() + var input = new ethUtil.BN(ethInWei, 10).toJSON() var result = util.formatBalance(input) - assert.equal(result, '1 ETH') + assert.equal(result, '1.0000 ETH') + }) + + it('should return eth as string followed by ETH', function() { + var input = new ethUtil.BN(ethInWei, 10).div(new ethUtil.BN('2', 10)).toJSON() + var result = util.formatBalance(input) + assert.equal(result, '.5000 ETH') + }) + + it('should display four decimal points', function() { + var input = "0x128dfa6a90b28000" + var result = util.formatBalance(input) + assert.equal(result, '1.3370 ETH') }) }) |