Skip to content

Commit

Permalink
add cc and ee
Browse files Browse the repository at this point in the history
  • Loading branch information
qwenode committed May 22, 2024
1 parent 07e6d52 commit 030490d
Show file tree
Hide file tree
Showing 7 changed files with 818 additions and 0 deletions.
175 changes: 175 additions & 0 deletions cc/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package cc

import "strconv"

// ToInt convert to int
func ToInt(str interface{}) int {
switch str.(type) {
case uint:
return int(str.(uint))
case uint64:
return int(str.(uint64))
case int:
return str.(int)
case int64:
return int(str.(int64))
case int32:
return int(str.(int32))
case uint32:
return int(str.(uint32))
case string:
atoi, _ := strconv.Atoi(str.(string))
return atoi
case float64:
return int(str.(float64))
case float32:
return int(str.(float32))
case bool:
if str.(bool) {
return 1
}
return 0
default:
return 0
}
}

// ToUInt convert to uint
func ToUInt(str interface{}) uint {
switch str.(type) {
case uint64:
return uint(str.(uint64))
case uint:
return str.(uint)
case int:
return uint(str.(int))
case int64:
return uint(str.(int64))
case int32:
return uint(str.(int32))
case uint32:
return uint(str.(uint32))
case string:
atoi, _ := strconv.Atoi(str.(string))
return uint(atoi)
case float64:
return uint(str.(float64))
case float32:
return uint(str.(float32))
default:
return 0
}
}

// ToUInt64 convert to uint64
func ToUInt64(str interface{}) uint64 {
switch str.(type) {
case uint64:
return str.(uint64)
case uint:
return uint64(str.(uint))
case int:
return uint64(str.(int))
case int64:
return uint64(str.(int64))
case int32:
return uint64(str.(int32))
case uint32:
return uint64(str.(uint32))
case string:
atoi, _ := strconv.Atoi(str.(string))
return uint64(atoi)
case float64:
return uint64(str.(float64))
case float32:
return uint64(str.(float32))
default:
return 0
}
}

// ToInt64 convert to int64
func ToInt64(str interface{}) int64 {
switch str.(type) {
case uint64:
return int64(str.(uint64))
case uint:
return int64(str.(uint))
case int:
return int64(str.(int))
case int32:
return int64(str.(int32))
case uint32:
return int64(str.(uint32))
case int64:
return str.(int64)
case string:
atoi, _ := strconv.Atoi(str.(string))
return int64(atoi)
case float64:
return int64(str.(float64))
case float32:
return int64(str.(float32))
default:
return 0
}
}

// ToInt32 convert to int32
func ToInt32(str interface{}) int32 {
switch str.(type) {
case uint64:
return int32(str.(uint64))
case uint:
return int32(str.(uint))
case int:
return int32(str.(int))
case int32:
return str.(int32)
case uint32:
return int32(str.(uint32))
case int64:
return int32(str.(int64))
case string:
atoi, _ := strconv.Atoi(str.(string))
return int32(atoi)
case float64:
return int32(str.(float64))
case float32:
return int32(str.(float32))
case bool:
if str.(bool) == true {
return 1
}
return 0
default:
return 0
}
}

// ToFloat64 convert to float64
func ToFloat64(str interface{}) float64 {
switch str.(type) {
case uint64:
return float64(str.(uint64))
case uint:
return float64(str.(uint))
case int:
return float64(str.(int))
case int32:
return float64(str.(int32))
case uint32:
return float64(str.(uint32))
case int64:
return float64(str.(int64))
case string:
float, _ := strconv.ParseFloat(ToString(str), 64)
return float
case float64:
return str.(float64)
case float32:
return float64(str.(float32))
default:
return float64(0)
}
}
Loading

0 comments on commit 030490d

Please sign in to comment.