This is a creation in Article, where the information may have evolved or changed.
Go StrConv Package _ string and numeric type conversions
Converting a string to a numeric type and Boolean type
package mainimport ("fmt""strconv")func main() {str1 := "0.23"f1, _ := strconv.ParseFloat(str1, 64)f2, _ := strconv.ParseFloat(str1, 32)fmt.Println(f1) //0.23fmt.Println(f2) //0.23fmt.Println(f2 * 100)str2 := "1234f" // 16进制的字符串// 将字符串解析为整数,ParseInt 支持正负号,ParseUint 不支持正负号。// base 表示进位制(2 到 36),如果 base 为 0,则根据字符串前缀判断,// 前缀 0x 表示 16 进制,前缀 0 表示 8 进制,否则是 10 进制。// bitSize 表示结果的位宽(包括符号位),0 表示最大位宽。n, err := strconv.ParseInt(str2, 16, 32)if err != nil {fmt.Println(err)}fmt.Printf("%x\n", n)str3 := "12"c, _ := strconv.ParseUint(str3, 10, 32)fmt.Println(c)str4 := "true"// 将字符串转换为布尔值// 它接受真值:1, t, T, TRUE, true, True// 它接受假值:0, f, F, FALSE, false, False// 其它任何值都返回一个错误。b, _ := strconv.ParseBool(str4)if b {fmt.Println("ParseBool")}str5 := "12345"// 将字符串转换为十进制整数,即:ParseInt(s, 10, 0) 的简写)// alphanumeric to integeri, _ := strconv.Atoi(str5)fmt.Println(i)}
Convert floating-point numbers to strings
Go provides two size floating-point numbers, float32 and float64.
Their arithmetic specifications are defined by the IEEE754 International Standard, and modern CPUs have implemented this specification. The maximum range that can be represented is
fmt.Printf("%e\n", math.MaxFloat32)fmt.Printf("%e\n", math.MaxFloat64)
Use FMT. SPRINTF can format a floating-point number as a string,
%b is a scientific notation of the power of the exponent two without fractional parts, with StrConv. The formatfloat ' B ' conversion format is consistent. such as -123456p-78
%e scientific counting method, for example -1234.456e+78
%E scientific counting method, for example -1234.456E+78
%f has a decimal point but no index, for example 123.456
%g Select%e or%f as appropriate to produce a more compact (no end of 0) output
%G Select%E or%f as appropriate to produce a more compact (no end of 0) output
Example
package mainimport ("fmt")func main() {f := 3.1415926str1 := fmt.Sprintf("%f", f)// 使用小数点后7位的精度进行打印,打印宽度是8个字符str2 := fmt.Sprintf("%8.7f", f)fmt.Println(str1) //3.141593fmt.Println(str2)str3 := fmt.Sprintf("%e", f)fmt.Println(str3)str4 := fmt.Sprintf("%g", f)fmt.Println(str4)}
Run as follows,
3.1415933.14159263.141593e+003.1415926
It is important to note that the default rounding rules for Golang floating-point types are as follows,
package mainimport ("fmt")func main() {fmt.Printf("9.8249=>%0.2f(四舍)\n", 9.8249)fmt.Printf("9.82671=>%0.2f(六入)\n", 9.82671)fmt.Printf("9.8351=>%0.2f(五后非零就进一)\n", 9.8351)fmt.Printf("9.82501=>%0.2f(五后非零就进一)\n", 9.82501)fmt.Printf("9.8250=>%0.2f(五后为零看奇偶,五前为偶应舍去)\n", 9.8250)fmt.Printf("9.8350=>%0.2f(五后为零看奇偶,五前为奇要进一)\n", 9.8350)}
You can also use StrConv. The Formatfloat function converts floating-point numbers to strings.
package mainimport ("strconv""fmt")func main() {f := 3.1415926// FormatFloat 将浮点数 f 转换为字符串形式// f:要转换的浮点数// fmt:格式标记(b、e、E、f、g、G)// prec:精度(数字部分的长度,不包括指数部分)// bitSize:指定浮点类型(32:float32、64:float64),结果会据此进行舍入。//// 格式标记:// 'b' (-ddddp±ddd,二进制指数)// 'e' (-d.dddde±dd,十进制指数)// 'E' (-d.ddddE±dd,十进制指数)// 'f' (-ddd.dddd,没有指数)// 'g' ('e':大指数,'f':其它情况)// 'G' ('E':大指数,'f':其它情况)//// 如果格式标记为 'e','E'和'f',则 prec 表示小数点后的数字位数// 如果格式标记为 'g','G',则 prec 表示总的数字位数(整数部分+小数部分)// 参考格式化输入输出中的旗标和精度说明str1 := strconv.FormatFloat(f, 'f', 2, 64)fmt.Println(str1) //3.14str2 := strconv.FormatFloat(f, 'f', -1, 64)fmt.Println(str2) //3.1415926}
Convert shaping to a string
package mainimport ("fmt""strconv")func main() {i1 := 209878// 将整数转换为字符串形式。base 表示转换进制,取值在 2 到 36 之间。// 结果中大于 10 的数字用小写字母 a - z 表示。str1 := strconv.FormatInt(int64(i1), 10)str2 := strconv.FormatInt(int64(i1), 16)fmt.Println(str1)fmt.Println(str2)// 将整数转换为十进制字符串形式(即:FormatInt(i, 10) 的简写)str3 := strconv.Itoa(i1)fmt.Println(str3)}
Append various types to strings after the DST trailer
package mainimport ("strconv""fmt")func main() {str := "hello world"dst := []byte(str)dst = append(dst, '-')dst = strconv.AppendBool(dst, true)dst = append(dst, '-')dst = strconv.AppendInt(dst, 12, 10)dst = append(dst, '-')dst = strconv.AppendFloat(dst, 3.1415926, 'f', 2, 64)fmt.Println(string(dst))}
==========end==========