aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/util.js
diff options
context:
space:
mode:
authorZac Mitton <zacmitton22@gmail.com>2016-05-26 09:29:03 +0800
committerZac Mitton <zacmitton22@gmail.com>2016-05-26 09:29:03 +0800
commit2ce9f1a776ea3168438fa2bdfcac90b35492e8ac (patch)
treeaed56176ef6d1c06b8a841efc105fa191b1bbb10 /ui/app/util.js
parentb2b7e7ff7e57a5ce300f9fcc0b5a621004f01ced (diff)
downloadtangerine-wallet-browser-2ce9f1a776ea3168438fa2bdfcac90b35492e8ac.tar
tangerine-wallet-browser-2ce9f1a776ea3168438fa2bdfcac90b35492e8ac.tar.gz
tangerine-wallet-browser-2ce9f1a776ea3168438fa2bdfcac90b35492e8ac.tar.bz2
tangerine-wallet-browser-2ce9f1a776ea3168438fa2bdfcac90b35492e8ac.tar.lz
tangerine-wallet-browser-2ce9f1a776ea3168438fa2bdfcac90b35492e8ac.tar.xz
tangerine-wallet-browser-2ce9f1a776ea3168438fa2bdfcac90b35492e8ac.tar.zst
tangerine-wallet-browser-2ce9f1a776ea3168438fa2bdfcac90b35492e8ac.zip
correctly displaying small balances with tests
Diffstat (limited to 'ui/app/util.js')
-rw-r--r--ui/app/util.js44
1 files changed, 28 insertions, 16 deletions
diff --git a/ui/app/util.js b/ui/app/util.js
index 81a029350..91f85e43f 100644
--- a/ui/app/util.js
+++ b/ui/app/util.js
@@ -84,29 +84,41 @@ function weiToEth(bn) {
}
// Takes hex, returns [beforeDecimal, afterDecimal]
-function parseBalance(balance, decimalsToKeep) {
- if (decimalsToKeep === undefined) decimalsToKeep = 4
- if (!balance || balance === '0x0') return ['0', '']
- var wei = numericBalance(balance)
- var padded = wei.toString(10)
- var len = padded.length
- var match = padded.match(/[^0]/)
- var nonZeroIndex = match && match.index
- var beforeDecimal = padded.substr(nonZeroIndex ? nonZeroIndex : 0, len - 18) || '0'
- var afterDecimal = padded.substr(len - 18, decimalsToKeep)
+function parseBalance(balance) {
+ if (!balance || balance === '0x0') return ['0', '0']
+ var wei = numericBalance(balance).toString(10)
+ var eth = String(wei/valueTable['wei'])
+ var beforeDecimal = String(Math.floor(eth))
+ var afterDecimal
+ if(eth.indexOf('.') > -1){
+ afterDecimal = eth.slice(eth.indexOf('.') + 1)
+ }else{
+ afterDecimal = '0'
+ }
return [beforeDecimal, afterDecimal]
}
// Takes wei hex, returns "None" or "${formattedAmount} ETH"
-function formatBalance(balance) {
+function formatBalance(balance, decimalsToKeep) {
var parsed = parseBalance(balance)
var beforeDecimal = parsed[0]
var afterDecimal = parsed[1]
- if (beforeDecimal === '0' && afterDecimal === '') return 'None'
- var result = beforeDecimal
- if (afterDecimal) result += '.'+afterDecimal
- result += ' ETH'
- return result
+ var formatted = "None"
+ if(decimalsToKeep === undefined){
+ if(beforeDecimal === '0'){
+ if(afterDecimal !== '0'){
+ var sigFigs = afterDecimal.match(/^0*(.{2})/) //default: grabs 2 most significant digits
+ if(sigFigs){afterDecimal = sigFigs[0]}
+ formatted = '0.' + afterDecimal + ' ETH'
+ }
+ }else{
+ formatted = beforeDecimal + "." + afterDecimal.slice(0,3) + ' ETH'
+ }
+ }else{
+ afterDecimal += Array(decimalsToKeep).join("0")
+ formatted = beforeDecimal + "." + afterDecimal.slice(0,decimalsToKeep) + ' ETH'
+ }
+ return formatted
}
function dataSize(data) {