-
Notifications
You must be signed in to change notification settings - Fork 1
/
change.go
80 lines (66 loc) · 1.71 KB
/
change.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package function
import (
"fmt"
"strconv"
"strings"
)
// Int64ToInt int64转int
func Int64ToInt(i64 int64) int {
strInt64 := strconv.FormatInt(i64, 10)
i, _ := strconv.Atoi(strInt64)
return i
}
// IntToInt64 int转int64
func IntToInt64(i int) int64 {
return int64(i)
}
// Int64ToString int64转string
func Int64ToString(i64 int64) string {
return strconv.FormatInt(i64, 10)
}
// Float64ToString float64转string
func Float64ToString(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}
// Float32ToString float32转string
func Float32ToString(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 32)
}
// StringToInt64 string转int64
func StringToInt64(str string) (int64, error) {
return strconv.ParseInt(str, 10, 64)
}
// StringToFloat64 string转float64
func StringToFloat64(s string) (float64, error) {
return strconv.ParseFloat(s, 64)
}
// Float64ToInt64 float64转int64
func Float64ToInt64(f float64) int64 {
return int64(f)
}
// Int64ToFloat64 int64转float64
func Int64ToFloat64(i64 int64) float64 {
return float64(i64)
}
// RemoveInvalid0 去掉无效的0
func RemoveInvalid0(f float64) float64 {
f64, _ := StringToFloat64(RemoveInvalid0ToString(f))
return f64
}
// RemoveInvalid0ToString 去掉无效的0并转为字符串格式
func RemoveInvalid0ToString(f float64) string {
return fmt.Sprintf("%g", f)
}
// ChineseToUnicode 中文转Unicode
func ChineseToUnicode(str string) string {
textQuoted := strconv.QuoteToASCII(str)
return textQuoted[1 : len(textQuoted)-1]
}
// UnicodeToChinese Unicode转中文
func UnicodeToChinese(s string) (string, error) {
str, err := strconv.Unquote(strings.Replace(strconv.Quote(s), `\\u`, `\u`, -1))
if err != nil {
return "", err
}
return str, nil
}