diff --git a/cointop/coins_table.go b/cointop/coins_table.go index 116d205..222b778 100644 --- a/cointop/coins_table.go +++ b/cointop/coins_table.go @@ -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, diff --git a/cointop/portfolio.go b/cointop/portfolio.go index cbc8d69..58ab85c 100644 --- a/cointop/portfolio.go +++ b/cointop/portfolio.go @@ -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) } diff --git a/cointop/price.go b/cointop/price.go index c75e2dc..5a6befc 100644 --- a/cointop/price.go +++ b/cointop/price.go @@ -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) +} diff --git a/cointop/price_alerts.go b/cointop/price_alerts.go index be299cd..4d7a83a 100644 --- a/cointop/price_alerts.go +++ b/cointop/price_alerts.go @@ -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) diff --git a/pkg/api/util/util.go b/pkg/api/util/util.go index 4621d27..ffb52db 100644 --- a/pkg/api/util/util.go +++ b/pkg/api/util/util.go @@ -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) } diff --git a/pkg/humanize/humanize_test.go b/pkg/humanize/humanize_test.go new file mode 100644 index 0000000..e7c3de8 --- /dev/null +++ b/pkg/humanize/humanize_test.go @@ -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() + } +}