From dda4a8dc6385ea04f2359858618fc07bc1829038 Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Wed, 7 Aug 2019 23:36:53 +0300 Subject: [PATCH] Fix metrics node balance update issue --- api/service/metrics/service.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/api/service/metrics/service.go b/api/service/metrics/service.go index 700508d33..46cd2ac8d 100644 --- a/api/service/metrics/service.go +++ b/api/service/metrics/service.go @@ -2,6 +2,7 @@ package metrics import ( "fmt" + "math" "math/big" "net" "net/http" @@ -158,19 +159,9 @@ func (s *Service) Run() { // FormatBalance formats big.Int balance with precision. func FormatBalance(balance *big.Int) float64 { - stringBalance := balance.String() - if len(stringBalance) < BalanceScale { - return 0.0 - } - if len(stringBalance) == BalanceScale { - stringBalance = "0." + stringBalance[len(stringBalance)-BalanceScale:len(stringBalance)-BalancePrecision] - } else { - stringBalance = stringBalance[:len(stringBalance)-BalanceScale] + "." + stringBalance[len(stringBalance)-BalanceScale:len(stringBalance)-BalancePrecision] - } - if res, err := strconv.ParseFloat(stringBalance, 64); err == nil { - return res - } - return 0.0 + scaledBalance := new(big.Float).Quo(new(big.Float).SetInt(balance), new(big.Float).SetFloat64(math.Pow10(BalanceScale))) + floatBalance, _ := scaledBalance.Float64() + return floatBalance } // UpdateBlockHeight updates block height.