Go languages string int int64 float conversion methods between types

  • 2020-06-03 06:53:50
  • OfStack

(1) turn string int


s := strconv.Itoa(i)
 Is equivalent to s := strconv.FormatInt(int64(i), 10)

(2) turn string int64


i := int64(123)
s := strconv.FormatInt(i, 10)

The second parameter is the cardinality and can be chosen from 2 to 36

Note: For unsigned plastic, it can be used FormatUint(i uint64, base int)

(3) turn int string


i, err := strconv.Atoi(s)

(4) turn int64 string


i, err := strconv.ParseInt(s, 10, 64)

The second parameter is cardinality (2-36), and the third parameter bit size represents the expected conversion result type. Its values can be 0, 8, 16, 32 and 64, corresponding to int, int8, int16, int32 and int64 respectively

(5) float related

Turn float string:


v := 3.1415926535
s1 := strconv.FormatFloat(v, 'E', -1, 32)//float32s2 := strconv.FormatFloat(v, 'E', -1, 64)//float64

Function prototypes and the parameter meaning specific to view: https: / / golang org/pkg/strconv / # FormatFloat

Turn string float:


s := "3.1415926535"
v1, err := strconv.ParseFloat(v, 32)
v2, err := strconv.ParseFloat(v, 64)

PS: go language string, int, int64 interchangeover


//string to int 
int,err:=strconv.Atoi(string) 
//string to int64 
int64, err := strconv.ParseInt(string, 10, 64) 
//int to string 
string:=strconv.Itoa(int) 
//int64 to string 
string:=strconv.FormatInt(int64,10)
//string to float32(float64)
float,err := strconv.ParseFloat(string,32/64)
//float to string
string := strconv.FormatFloat(float32, 'E', -1, 32)
string := strconv.FormatFloat(float64, 'E', -1, 64)
// 'b' (-ddddp Plus or minus ddd . 2 Hexadecimal index )
// 'e' (-d.dddde Plus or minus dd . 10 Hexadecimal index )
// 'E' (-d.ddddE Plus or minus dd . 10 Hexadecimal index )
// 'f' (-ddd.dddd There is no exponent )
// 'g' ('e': Big index, 'f': Other situations )
// 'G' ('E': Big index, 'f': Other situations )

conclusion


Related articles: