improve price formatter

This commit is contained in:
c9s 2020-08-06 13:42:50 +08:00
parent bfe38b8815
commit cb2ee2d502
2 changed files with 20 additions and 1 deletions

View File

@ -1,10 +1,15 @@
package types
import (
"github.com/leekchan/accounting"
"math"
"strconv"
)
var USD = accounting.Accounting{Symbol: "$ ", Precision: 2}
var BTC = accounting.Accounting{Symbol: "BTC ", Precision: 2}
var BNB = accounting.Accounting{Symbol: "BNB ", Precision: 4}
type Market struct {
Symbol string
PricePrecision int
@ -17,6 +22,20 @@ type Market struct {
}
func (m Market) FormatPrice(val float64) string {
switch m.QuoteCurrency {
case "USD", "USDT":
return USD.FormatMoneyFloat64(val)
case "BTC":
return BTC.FormatMoneyFloat64(val)
case "BNB":
return BNB.FormatMoneyFloat64(val)
}
return strconv.FormatFloat(val, 'f', m.PricePrecision, 64)
}

View File

@ -53,7 +53,7 @@ func (trade Trade) SlackAttachment() slack.Attachment {
{Title: "Side", Value: trade.Side, Short: true},
{Title: "Price", Value: market.FormatPrice(trade.Price), Short: true},
{Title: "Volume", Value: market.FormatVolume(trade.Quantity), Short: true},
{Title: "Amount", Value: market.FormatPrice(trade.QuoteQuantity), Short: true},
{Title: "Amount", Value: market.FormatPrice(trade.QuoteQuantity)},
{Title: "FeeCurrency", Value: trade.FeeCurrency, Short: true},
{Title: "Fee", Value: util.FormatFloat(trade.Fee, 4), Short: true},
},