mirror of
https://github.com/wahyd4/scraper.git
synced 2026-08-09 05:06:22 +10:00
fix tess
This commit is contained in:
+10
-8
@@ -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
@@ -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,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user