go package 學習筆記 —— strconv(string與其餘基本數據類型(int, float, bool)的轉換)

strconv實現了go中基本數據類型與string之間的轉換。函數

How to use in go

go doc:https://godoc.org/strconvui

import "strconv"orm

int ↔ string

func Atoi(s string) (int, error)

  將string類型的s轉換爲十進制int類型,同時會返回error。blog

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。get

func FormatInt(i int64, base int) string

  將i轉換爲base進制下的string類型,base的的取值範圍是[2,36]。string

// 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類型。it

func FormatUint(i uint64, base int) string

  將uint64類型按照給定base轉換爲string類型,base的取值範圍是[2,36]。class

// 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。import

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
相關文章
相關標籤/搜索