From 127dc0d250ea59b82333ac89f5ad6d046a50d933 Mon Sep 17 00:00:00 2001 From: sre Date: Fri, 17 Jun 2022 14:36:11 +0800 Subject: [PATCH] convert --- convert/entry.go | 4 +++ convert/entry_test.go | 3 ++ convert/int.go | 22 +++++++++++++ convert/int_test.go | 11 +++++++ convert/str.go | 76 +++++++++++++++++++++++++++++++++++++++++++ convert/str_test.go | 19 +++++++++++ gtool.go | 2 ++ 7 files changed, 137 insertions(+) create mode 100644 convert/entry.go create mode 100644 convert/entry_test.go create mode 100644 convert/int.go create mode 100644 convert/int_test.go create mode 100644 convert/str.go create mode 100644 convert/str_test.go diff --git a/convert/entry.go b/convert/entry.go new file mode 100644 index 0000000..a72cfce --- /dev/null +++ b/convert/entry.go @@ -0,0 +1,4 @@ +package convert + +type Conv struct { +} diff --git a/convert/entry_test.go b/convert/entry_test.go new file mode 100644 index 0000000..271dec5 --- /dev/null +++ b/convert/entry_test.go @@ -0,0 +1,3 @@ +package convert + +var conv Conv diff --git a/convert/int.go b/convert/int.go new file mode 100644 index 0000000..2f597b5 --- /dev/null +++ b/convert/int.go @@ -0,0 +1,22 @@ +package convert + +import "strconv" + +// IntToStr int类型转字符串 +// num int类型的数据,不区分int64或者int类型 +func (c *Conv) IntToStr(num any) string { + var str string + switch num.(type) { + case int: + str = strconv.Itoa(num.(int)) + case int64: + str = strconv.FormatInt(num.(int64), 10) + case int32: + str = strconv.FormatInt(int64(num.(int32)), 10) + case string: + str = num.(string) + default: + str = "" + } + return str +} diff --git a/convert/int_test.go b/convert/int_test.go new file mode 100644 index 0000000..bc7f3d1 --- /dev/null +++ b/convert/int_test.go @@ -0,0 +1,11 @@ +package convert + +import ( + "fmt" + "testing" +) + +func TestConv_IntToStr(t *testing.T) { + fmt.Println(conv.IntToStr(3455675656) + "okok") + +} diff --git a/convert/str.go b/convert/str.go new file mode 100644 index 0000000..e559b41 --- /dev/null +++ b/convert/str.go @@ -0,0 +1,76 @@ +package convert + +import "strconv" + +// StrToInt 字符串转int类型,当存在defaultNum时,出现异常会返回设置的默认值,若不出现异常正常返回 +func (c *Conv) StrToInt(str string, defaultNum ...int) int { + num, err := strconv.Atoi(str) + if err != nil { + if len(defaultNum) > 0 { + return defaultNum[0] + } else { + return 0 + } + } + return num +} + +// StrToInt64 StrNum需要转换的字符串 +// defaultNum默认值 +// String类型转int64 +func (c *Conv) StrToInt64(strNum string, defaultNum ...int64) int64 { + num, err := strconv.ParseInt(strNum, 10, 64) + if err != nil { + if len(defaultNum) > 0 { + return defaultNum[0] + } else { + return 0 + } + } + return num +} + +// StrToInt32 StrNum需要转换的字符串 +// defaultNum默认值 +// String类型转int64 +func (c *Conv) StrToInt32(strNum string, defaultNum ...int32) int32 { + num, err := strconv.ParseInt(strNum, 10, 32) + if err != nil { + if len(defaultNum) > 0 { + return defaultNum[0] + } else { + return 0 + } + } + return int32(num) +} + +// StrToFloat32 String转float32位 +// strFloat 需要转的字符串 +// defaultFloat 默认值,若没有出现转换异常直接返回0.0 +func (c *Conv) StrToFloat32(strFloat string, defaultFloat ...float32) float32 { + float, err := strconv.ParseFloat(strFloat, 32) + if err != nil { + if len(defaultFloat) > 0 { + return defaultFloat[0] + } else { + return 0.0 + } + } + return float32(float) +} + +// StrToFloat64 String转float64位 +// strFloat 需要转的字符串 +// defaultFloat 默认值,若没有出现转换异常直接返回0.0 +func (c *Conv) StrToFloat64(strFloat string, defaultFloat ...float64) float64 { + float, err := strconv.ParseFloat(strFloat, 36) + if err != nil { + if len(defaultFloat) > 0 { + return defaultFloat[0] + } else { + return 0.0 + } + } + return float +} diff --git a/convert/str_test.go b/convert/str_test.go new file mode 100644 index 0000000..1e20365 --- /dev/null +++ b/convert/str_test.go @@ -0,0 +1,19 @@ +package convert + +import ( + "fmt" + "testing" +) + +func TestConv_StrToInt(t *testing.T) { + fmt.Println(conv.StrToInt("345")) + fmt.Println(conv.StrToInt("3r45")) + fmt.Println(conv.StrToInt("3r45", 45)) + +} +func TestConv_StrToInt64(t *testing.T) { + fmt.Println(conv.StrToInt64("34567")) + fmt.Println(conv.StrToInt64("34ttt567")) + fmt.Println(conv.StrToInt64("34ttt567", 789)) + +} diff --git a/gtool.go b/gtool.go index 25de909..6240858 100644 --- a/gtool.go +++ b/gtool.go @@ -1,6 +1,7 @@ package gtool import ( + "git.sre.ink/go/gtool/convert" "git.sre.ink/go/gtool/crypto" "git.sre.ink/go/gtool/rand" "git.sre.ink/go/gtool/str" @@ -11,4 +12,5 @@ var ( Str str.Str Crypt crypto.Crypto Rand rand.Rand + Conv convert.Conv )