This commit is contained in:
2020-03-03 23:17:59 +11:00
parent c7923bafa6
commit 22d3cae9b2
2 changed files with 14 additions and 12 deletions
+10 -8
View File
@@ -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
+4 -4
View File
@@ -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,
},