go package 学习笔记 —— strconv(string与其他基本数据类型(int, float, bool)的转换)
strconv实现了go中基本数据类型与string之间的转换。
How to use in go
go doc:https://godoc.org/strconv
import "strconv"
int ↔ string
func Atoi(s string) (int, error)
将string类型的s转换为十进制int类型,同时会返回error。
func Itoa(i int) string
将十进制i转换为string类型。
// int ↔ string
stringValue := "150"
intValue, err := strconv.Atoi(stringValue)
if err != nil {
fmt.Printf("Failed to conv string to int: %s\n", err)
} else {
fmt.Printf("%T %s conv to %T %d\n", stringValue, stringValue, intValue, intValue) //string 150 conv to int 150
}
stringValue = strconv.Itoa(intValue)
fmt.Printf("%T %d conv to %T %s\n", intValue, intValue, stringValue, stringValue) //int 150 conv to string 150
int64 ↔ string
func ParseInt(s string, base int, bitSize int) (i int64, err error)
将string类型的s转换为base进制下bitSize比特的i,base的取值范围是{0}∪[2,36],bitSize的取值范围是[0,64]。当base=0时,字符串的前缀表示基数,比如“0x”表示十六进制,“0”表示八进制,其他表示十进制;当base=1 || base<0 || base>36,返回error。bitSize指定了整型的位数,bitSize=0,8,16,32和64分别对应了int,int8,int16,int32和int64。
func FormatInt(i int64, base int) string
将i转换为base进制下的string类型,base的的取值范围是[2,36]。
// int64 ↔ string
int64Value, err := strconv.ParseInt(stringValue, 10, 64)
if err != nil {
fmt.Printf("Failed to conv string to int64: %s\n", err)
} else {
fmt.Printf("%T %s conv to %T %d\n", stringValue, stringValue, int64Value, int64Value) //string 150 conv to int64 150
}
stringValue = strconv.FormatInt(int64Value, 10)
fmt.Printf("%T %d conv to %T %s\n", int64Value, int64Value, stringValue, stringValue) //int64 150 conv to string 150
uint64 ↔ string
func ParseUint(s string, base int, bitSize int) (uint64, error)
ParseUint的用法与ParseInt相同,返回的结果是uint64类型。
func FormatUint(i uint64, base int) string
将uint64类型按照给定base转换为string类型,base的取值范围是[2,36]。
// uint64 ↔ string
uint64Value, err := strconv.ParseUint(stringValue, 10, 64)
if err != nil {
fmt.Printf("Failed to conv string to uint64: %s\n", err)
} else {
fmt.Printf("%T %s conv to %T %d\n", stringValue, stringValue, uint64Value, uint64Value) //string 150 conv to uint64 150
}
stringValue = strconv.FormatUint(uint64Value, 10)
fmt.Printf("%T %d conv to %T %s\n", uint64Value, uint64Value, stringValue, stringValue) //uint64 150 conv to string 150
float64 ↔ string
func ParseFloat(s string, bitSize int) (float64, error)
ParseFloat将字符串s转换为浮点数,精度由bitSize指定,bitSize=32转换为float32,64转换为float64。 当bitSize = 32时,结果仍然是float64类型,但不改变值就可以转换为float32。
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
FormatFloat根据格式fmt和精度prec将浮点数f转换为字符串。
格式fmt取值有几种,'b'表示二进制指数( - ddddp±ddd),'e'表示十进制指数( - d.dddde±dd),'E'表示十进制指数(- ddddd±dd),'f'表示无指数(- ddd.dddd),'g'表示对于大指数使用格式'e',否则使用'f','G'表示对于大指数使用格式'E',否则使用'f'。
精度是由格式'e','E','f','g'和'G'控制。 对于'e','E'和'f',是小数点后的位数。 对于'g'和'G',是最大有效位数(删除末尾的零)。当prec=-1时,表示使用所需的最小位数。
// float64 ↔ string
float64Value,err:=strconv.ParseFloat("3.1415926535",64)
if err !=nil{
fmt.Printf("Failed to conv string to float64: %s",err)
}else{
fmt.Printf("%T %s conv to %T %v\n","3.1415926535","3.1415926535",float64Value,float64Value) //string 3.1415926535 conv to float64 3.1415926535
}
stringValue=strconv.FormatFloat(float64Value,'E',-1,64)
fmt.Printf("%T %v conv to %T %s\n",float64Value,float64Value,stringValue,stringValue) //float64 3.1415926535 conv to string 3.1415926535E+00
bool ↔ string
func ParseBool(str string) (bool, error)
返回string类型的str表示的bool值,str∈{1,t,T,TRUE,true,True,0,f,F,FALSE,false,False},如果str是其他值,函数返回error。
func FormatBool(b bool) string
根据b的值返回“true”或“false”。
// bool ↔ string
boolValue, err := strconv.ParseBool("true")
if err != nil {
fmt.Printf("Failed to conv string to bool: %s\n", err)
} else {
fmt.Printf("%T %s conv to %T %v\n", "true", "true", boolValue, boolValue) //string true conv to bool true
}
stringValue = strconv.FormatBool(boolValue)
fmt.Printf("%T %v conv to %T %s\n", boolValue, boolValue, stringValue, stringValue) //bool true conv to string true
最新文章
- ElasticSearch 5学习(10)——结构化查询(包括新特性)
- Android之动画的学习(转载)
- table 细边框
- Windows 常用 CMD 命令行介绍
- koala不支持中文的解决办法(问题出现在使用中文字体时报错)
- PHP 解析 ElasticSearch 的 json 方法,有關遍歷所有 json 元素
- 第一节 HTML网页和CSS样式
- rbd命令
- DRAM 内存介绍(二)
- 微信公众平台网页获取用户OpenID方法
- 最近在用placeholder ,是已有的,网上也有不少都是jq写的
- Android EditText载入HTML内容(内容包括网络图片)
- Form.KeyPreview 属性
- [LeetCode OJ] Decode Ways
- mvc mvp mvvm区别
- IE按F12,开发者工具已经在最低点,那么国家就不会出错
- Rythm.js 使用教程详解
- python 学习第一天
- HTML基础之CSS
- WM_CONCAT和LISTAGG 语法例子