-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
56 lines (50 loc) · 1.29 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package bdstockexchange
import (
"log"
"strconv"
"strings"
)
// isValidCategoryName checks if the user input catergory is valid
func isValidCategoryName(categoryName string) bool {
if categoryName == "A" || categoryName == "B" || categoryName == "G" || categoryName == "N" || categoryName == "Z" {
return true
}
return false
}
// normalizeAmerican return the cleaned string if the input string is in american format ex : 10,000
func normalizeAmerican(old string) string {
return strings.Replace(old, ",", "", -1)
}
// toFloat64 returns the float64 cleaning the input string
func toFloat64(text string) float64 {
if text == "--" {
text = strings.Replace(text, "--", "0", -1)
}
if text == "N/A" {
text = strings.Replace(text, "N/A", "0", -1)
}
val, err := strconv.ParseFloat(normalizeAmerican(text), 64)
if err != nil {
log.Fatal(err)
}
return val
}
// toInt64 returns the int64 cleaning the input string
func toInt64(text string) int64 {
if strings.Contains(text, " ") {
text = strings.Replace(text, " ", "", -1)
}
val, err := strconv.ParseInt(normalizeAmerican(text), 10, 64)
if err != nil {
log.Println(err)
}
return val
}
// toInt parse the int from a input string
func toInt(text string) int {
val, err := strconv.Atoi(text)
if err != nil {
log.Fatal(err)
}
return val
}