course/strconv/strconv.go

44 lines
844 B
Go
Raw Normal View History

2024-07-03 02:05:02 +08:00
package main
import (
"fmt"
"strconv"
)
func main() {
//从字符串解析出整形
str := "10000"
retInt64, err := strconv.ParseInt(str, 10, 64) //均转换成int64
if err != nil {
fmt.Println(err)
}
fmt.Printf("%v,%T\n", retInt64, retInt64)
//转换成int类型
retInt, _ := strconv.Atoi(str)
fmt.Printf("%v,%T\n", retInt, retInt)
//将数字转换成字符串类型
i := int32(97)
ret := fmt.Sprintf("%d", i)
fmt.Printf("%#v\n", ret)
//数字转换成字符串类型
iInt := int(i)
retStr := strconv.Itoa(iInt)
fmt.Printf("%#v\n", retStr)
//字符串转换成bool类型
boolstr := "true"
boolV, _ := strconv.ParseBool(boolstr)
fmt.Printf("%v,%T\n", boolV, boolV)
//字符串解析成浮点类型
floatstr := "1.234"
floatV, _ := strconv.ParseFloat(floatstr, 64)
fmt.Printf("%v,%T\n", floatV, floatV)
}