github.com/256dpi/max-go@v0.7.0/utils.go (about) 1 package max 2 3 import "strconv" 4 5 // ToInt will convert to int64. 6 func ToInt(atom Atom) int64 { 7 switch v := atom.(type) { 8 case int64: 9 return v 10 case float64: 11 return int64(v) 12 case string: 13 n, _ := strconv.ParseInt(v, 10, 64) 14 return n 15 default: 16 return 0 17 } 18 } 19 20 // ToFloat will convert to float64. 21 func ToFloat(atom Atom) float64 { 22 switch v := atom.(type) { 23 case int64: 24 return float64(v) 25 case float64: 26 return v 27 case string: 28 n, _ := strconv.ParseFloat(v, 64) 29 return n 30 default: 31 return 0 32 } 33 } 34 35 // ToString will convert to string. 36 func ToString(atom Atom) string { 37 switch v := atom.(type) { 38 case int64: 39 return strconv.FormatInt(v, 10) 40 case float64: 41 return strconv.FormatFloat(v, 'f', -1, 64) 42 case string: 43 return v 44 default: 45 return "" 46 } 47 }