(1) int to string
?
12 |
s := strconv.Itoa(i) 等价于s := strconv.FormatInt(int64(i), 10) |
(2) Int64 turn string
?
12 |
i := int64(123) s := strconv.FormatInt(i, 10) |
The second parameter is a radix, optional 2~36
Note: For unsigned shaping, you can use theFormatUint(i uint64, base int)
(3) string to int
?
1 |
i, err := strconv.Atoi(s) |
(4) String transfer Int64
?
1 |
i, err := strconv.ParseInt(s, 10, 64) |
The second argument is cardinality (2~36), and the third parameter bit size represents the result type of the desired conversion, with a value of 0, 8, 16, 32, and 64, corresponding to int, int8, Int16, Int32, and Int64
(5) Float-related
Float to string:
?
12 |
v := 3.1415926535 s1 := strconv.FormatFloat(v, ‘E‘ , -1, 32) //float32s2 := strconv.FormatFloat(v, ‘E‘, -1, 64)//float64 |
function prototype and parameter meaning specific to view: https://golang.org/pkg/strconv/#FormatFloat
String to float:
?
123 |
s := "3.1415926535" v1, err := strconv.ParseFloat(v, 32) v2, err := strconv.ParseFloat(v, 64) |
Ps:go languages String, int, int64 convert each other
?
12345678910111213141516171819 |
//string到int
int,err:=strconv.Atoi(string)
//string到int64
int64, err := strconv.ParseInt(string, 10, 64)
//int到string
string:=strconv.Itoa(int)
//int64到string
string:=strconv.FormatInt(int64,10)
//string到float32(float64)
float,err := strconv.ParseFloat(string,32/64)
//float到string
string := strconv.FormatFloat(float32,
‘E‘
, -1, 32)
string := strconv.FormatFloat(float64,
‘E‘
, -1, 64)
// ‘b‘ (-ddddp±ddd,二进制指数)
// ‘e‘ (-d.dddde±dd,十进制指数)
// ‘E‘ (-d.ddddE±dd,十进制指数)
// ‘f‘ (-ddd.dddd,没有指数)
// ‘g‘ (‘e‘:大指数,‘f‘:其它情况)
// ‘G‘ (‘E‘:大指数,‘f‘:其它情况)
|
Summarize
The above is a small series to introduce you to the Go language String,int,int64, float type conversion method, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!
Articles you may be interested in:
- MongoDB error Tcmalloc:large alloc out of memory, printing stack and exiting workaround
- MongoDB exception: $concat only supports strings, not NUMBERINT32 solution
- Go Language Interface detailed
- Interface usage examples in the Go language
- Golang implementing Unicode conversion to string strings
Original link: http://www.cnblogs.com/vdvvdd/archive/2017/07/20/7211122.html
If you have questions about this article, please submit to the Exchange community, the vast number of enthusiastic netizens will answer for you!! Click to enter the community
Go language String,int,int64, float type conversion method