Increase number of decimals shown when price < 1. #132

pull/133/head
Miguel Mota 3 years ago
parent f5adceea65
commit 758e8367f7
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -122,7 +122,7 @@ func (ct *Cointop) GetCoinsTable() *table.Table {
Text: symbol,
})
case "price":
text := humanize.Monetaryf(coin.Price, 2)
text := ct.FormatPrice(coin.Price)
ct.SetTableColumnWidthFromString(header, text)
ct.SetTableColumnAlignLeft(header, false)
rowCells = append(rowCells,

@ -125,7 +125,7 @@ func (ct *Cointop) GetPortfolioTable() *table.Table {
Text: symbol,
})
case "price":
text := humanize.Monetaryf(coin.Price, 2)
text := ct.FormatPrice(coin.Price)
symbolPadding := 1
ct.SetTableColumnWidth(header, utf8.RuneCountInString(text)+symbolPadding)
ct.SetTableColumnAlignLeft(header, false)
@ -718,7 +718,7 @@ func (ct *Cointop) PrintHoldingsTable(options *TablePrintOptions) error {
item[i] = entry.Symbol
case "price":
if humanReadable {
item[i] = fmt.Sprintf("%s%s", symbol, humanize.Monetaryf(entry.Price, 2))
item[i] = fmt.Sprintf("%s%s", symbol, ct.FormatPrice(entry.Price))
} else {
item[i] = strconv.FormatFloat(entry.Price, 'f', -1, 64)
}

@ -2,6 +2,7 @@ package cointop
import (
"fmt"
"math"
"strings"
"github.com/miguelmota/cointop/pkg/api"
@ -75,3 +76,15 @@ func GetCoinPrices(config *PricesConfig) ([]string, error) {
return prices, nil
}
// FormatPrice formats the coin price number of decimals and currency format
func (ct *Cointop) FormatPrice(price float64) string {
decimals := 2
if price < 1 {
decimals = 8
}
if price == math.Trunc(price) {
decimals = 2
}
return humanize.Monetaryf(price, decimals)
}

@ -96,7 +96,7 @@ func (ct *Cointop) GetPriceAlertsTable() *table.Table {
})
case "target_price":
targetPrice := fmt.Sprintf("%s %s", entry.Operator, humanize.Monetaryf(entry.TargetPrice, 2))
targetPrice := fmt.Sprintf("%s %s", entry.Operator, ct.FormatPrice(entry.TargetPrice))
ct.SetTableColumnWidthFromString(header, targetPrice)
ct.SetTableColumnAlignLeft(header, false)
rowCells = append(rowCells, &table.RowCell{
@ -187,7 +187,7 @@ func (ct *Cointop) CheckPriceAlert(alert *PriceAlert) error {
}
var msg string
title := "Cointop Alert"
priceStr := fmt.Sprintf("%s%s (%s%s)", ct.CurrencySymbol(), humanize.Numericf(alert.TargetPrice, 2), ct.CurrencySymbol(), humanize.Monetaryf(coin.Price, 2))
priceStr := fmt.Sprintf("%s%s (%s%s)", ct.CurrencySymbol(), ct.FormatPrice(alert.TargetPrice), ct.CurrencySymbol(), humanize.Monetaryf(coin.Price, 2))
if alert.Operator == ">" {
if coin.Price > alert.TargetPrice {
msg = fmt.Sprintf("%s price is greater than %v", alert.CoinName, priceStr)

@ -60,7 +60,7 @@ func FormatRank(rank interface{}) int {
// FormatPrice formats the price value
func FormatPrice(price float64, convert string) float64 {
convert = strings.ToUpper(convert)
pricestr := fmt.Sprintf("%.2f", price)
pricestr := fmt.Sprintf("%.5f", price)
if convert == "ETH" || convert == "BTC" || price < 1 {
pricestr = fmt.Sprintf("%.8f", price)
}

@ -0,0 +1,12 @@
package humanize
import (
"testing"
)
// TestMonetary tests monetary formatting
func TestMonetary(t *testing.T) {
if Monetaryf(834142.3256, 2) != "834,142.3256" {
t.FailNow()
}
}
Loading…
Cancel
Save