Skip to content

Commit

Permalink
Merge pull request #338 from simelo/stdevAntiD2ta_t337_Fix_FormatCoin…
Browse files Browse the repository at this point in the history
…s_representation_of_small_numbers

Fixes #337 - FormatCoins problem with representations of numbers less than zero
  • Loading branch information
olemis committed Feb 8, 2020
2 parents 4c387b4 + 20d1203 commit 3ddcc55
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/models/walletsManager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models

import (
"fmt"
"sync"

"github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/params"
Expand Down Expand Up @@ -874,16 +873,21 @@ func fromWalletToQWallet(wlt core.Wallet, isEncrypted bool) *QWallet {
return qWallet
}

floatBl := float64(bl) / float64(accuracy)
qWallet.SetSky(fmt.Sprint(floatBl))
qWallet.SetSky(util.FormatCoins(bl, accuracy))

bl, err = wlt.GetCryptoAccount().GetBalance(sky.CoinHoursTicker)
if err != nil {
qWallet.SetCoinHours("N/A")
logWalletManager.WithError(err).Error("Couldn't get Coin Hours balance")
return qWallet
}
qWallet.SetCoinHours(fmt.Sprint(bl))
accuracy, err = util.AltcoinQuotient(params.CoinHoursTicker)
if err != nil {
qWallet.SetCoinHours("N/A")
logWalletManager.WithError(err).Error("Couldn't get Coin Hours Altcoin quotient")
return qWallet
}
qWallet.SetCoinHours(util.FormatCoins(bl, accuracy))

return qWallet
}
2 changes: 1 addition & 1 deletion src/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func FormatCoins(n uint64, quotient uint64) string {
lenQ := len(strconv.FormatUint(quotient, 10)) - 1
nFormatted := FormatUint64(n / quotient)
if lenQ > len(number) {
return nFormatted
return strconv.FormatFloat(float64(n)/float64(quotient), 'f', -1, 64)
}
reminder := number[len(number)-lenQ:]
reminder = RemoveZeros(reminder)
Expand Down
3 changes: 2 additions & 1 deletion src/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func TestFormatCoins(t *testing.T) {
{name: "format4", value: uint64(12340), result: "1.234", quotient: uint64(10000)},
{name: "format5", value: uint64(421142), result: "4,211.42", quotient: uint64(100)},
{name: "format6", value: uint64(0), result: "0", quotient: uint64(100)},
{name: "format7", value: uint64(42), result: "0", quotient: uint64(10000)},
{name: "format7", value: uint64(42), result: "0.0042", quotient: uint64(10000)},
{name: "format8", value: uint64(1000), quotient: uint64(1000000), result: ("0.001")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 3ddcc55

Please sign in to comment.