From 9d577ea0231802279ea0070a598f7dea9637f652 Mon Sep 17 00:00:00 2001 From: Frankie Date: Wed, 6 Jul 2016 17:04:09 -0700 Subject: Add decimal hendeling to ETH balance --- ui/app/util.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'ui/app/util.js') diff --git a/ui/app/util.js b/ui/app/util.js index db12a1282..d9afa1bba 100644 --- a/ui/app/util.js +++ b/ui/app/util.js @@ -99,22 +99,29 @@ function formatBalance (balance, decimalsToKeep) { var parsed = parseBalance(balance) var beforeDecimal = parsed[0] var afterDecimal = parsed[1] - 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' + var formatted, formattedBalance + + if (beforeDecimal === '0') { + if (afterDecimal !== '0') { + var sigFigs = afterDecimal.match(/^0*(.{2})/) // default: grabs 2 most significant digits + if (sigFigs) { afterDecimal = sigFigs[0] } + formattedBalance = `0.${afterDecimal.slice(0, 6)}` } } else { - afterDecimal += Array(decimalsToKeep).join('0') - formatted = beforeDecimal + '.' + afterDecimal.slice(0, decimalsToKeep) + ' ETH' + formattedBalance = `${beforeDecimal}.${afterDecimal.slice(0, 2)}` + } + if (decimalsToKeep) { + formattedBalance = `${beforeDecimal}.${afterDecimal.slice(0, decimalsToKeep)}` + } + + formatted = `${formattedBalance} ETH` + + if (formattedBalance === '0.0' || formattedBalance === undefined) { + formatted = 'None' + formattedBalance = 'None' } - return formatted + + return {formattedBalance, balance: parsed.join('.'), formatted} } function dataSize (data) { -- cgit