From 22d3cae9b23148be71fc0f310f5dda386ecbadbf Mon Sep 17 00:00:00 2001 From: Junwei Zhao Date: Tue, 3 Mar 2020 23:17:59 +1100 Subject: [PATCH] fix tess --- utils/utils.go | 18 ++++++++++-------- utils/utils_test.go | 8 ++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 42edbb9..799e487 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "fmt" + "math" "strconv" "strings" @@ -11,33 +12,34 @@ import ( const ( oneMillion = 1000000 oneThousand = 1000 - maxBid = ` max bi\` + maxBid = ` max bid` ) func ToMoney(priceString string) int { if !strings.HasPrefix(priceString, "$") { return 0 } + //remove $ priceWithoutDollar := priceString[1:] + priceWithoutDollar = strings.TrimSuffix(priceWithoutDollar, maxBid) + priceWithoutUnit := priceWithoutDollar[:len(priceWithoutDollar)-1] - if strings.HasSuffix(priceWithoutDollar, maxBid) { - priceWithoutUnit = priceWithoutUnit[:len(priceWithoutUnit)-len(maxBid)] - } fmt.Println(priceWithoutUnit) + floatPrice, err := strconv.ParseFloat(priceWithoutUnit, 10) if err != nil { log.Error().Msgf("cannot convert string to price data due to %v", err) return 0 } - if strings.HasSuffix(priceWithoutUnit, "m") { - return int(floatPrice * oneMillion) + if strings.HasSuffix(priceWithoutDollar, "m") { + return int(math.Round(floatPrice * oneMillion)) } - if strings.HasSuffix(priceWithoutUnit, "k") { - return int(floatPrice * oneThousand) + if strings.HasSuffix(priceWithoutDollar, "k") { + return int(math.Round(floatPrice * oneThousand)) } return 0 diff --git a/utils/utils_test.go b/utils/utils_test.go index f34c0e5..6970186 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -28,16 +28,16 @@ func Test_toMoney(t *testing.T) { 390000, }, { - "$2.01m max bi", + "$2.01m max bid", args{ - priceString: `$2.01m max bi\`, + priceString: `$2.01m max bid`, }, 2010000, }, { - `$890k max bi\`, + `$890k max bid`, args{ - priceString: `$890k max bi\`, + priceString: `$890k max bid`, }, 890000, },