go語(yǔ)言如何將整型轉(zhuǎn)為字符串

本篇內(nèi)容介紹了“go語(yǔ)言如何將整型轉(zhuǎn)為字符串”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

從策劃到設(shè)計(jì)制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶(hù)提供做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)、域名注冊(cè)、虛擬空間、網(wǎng)絡(luò)營(yíng)銷(xiāo)、VI設(shè)計(jì)、 網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。為客戶(hù)提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶(hù)的口碑塑造優(yōu)易品牌,攜手廣大客戶(hù),共同發(fā)展進(jìn)步。

轉(zhuǎn)換方法:1、用fmt包的Sprintf(),支持格式化變量轉(zhuǎn)為字符串,語(yǔ)法“fmt.Sprintf("%d", num)”;2、用strconv包的Itoa(),支持將int類(lèi)型轉(zhuǎn)換成字符串,語(yǔ)法“strconv.Itoa(num)”;3、用strconv包的FormatInt(),支持將int64類(lèi)型轉(zhuǎn)換成字符串,語(yǔ)法“strconv.FormatInt(num,10)”。

在實(shí)際開(kāi)發(fā)中我們往往需要對(duì)一些常用的數(shù)據(jù)類(lèi)型進(jìn)行轉(zhuǎn)換,如 string、int、int64、float 等數(shù)據(jù)類(lèi)型之間的轉(zhuǎn)換。

int整數(shù)轉(zhuǎn)字符串

1、fmt.Sprintf

fmt 包應(yīng)該是最常見(jiàn)的了,從剛開(kāi)始學(xué)習(xí) Golang 就接觸到了,寫(xiě) ‘hello, world’ 就得用它。它還支持格式化變量轉(zhuǎn)為字符串。%d 代表十進(jìn)制整數(shù)。

//Sprintf formats according to a format specifier and returns the resulting string.
func Sprintf(format string, a ...interface{}) string

使用示例:

str := fmt.Sprintf("%d", a)

2、strconv.Itoa

Go語(yǔ)言中的 strconv 包為我們提供了字符串和基本數(shù)據(jù)類(lèi)型之間的轉(zhuǎn)換功能。strconv 包中常用的函數(shù)包括 Atoi()、Itia()、parse 系列函數(shù)、format 系列函數(shù)、append 系列函數(shù)等。

其中Itoa()函數(shù)支持 int 類(lèi)型轉(zhuǎn)換成字符串

//Itoa is shorthand for FormatInt(int64(i), 10).
func Itoa(i int) string

使用示例:

func main() {
    num := 100
    str := strconv.Itoa(num)
    fmt.Printf("type:%T value:%#v\n", str, str)
}

運(yùn)行結(jié)果如下所示:

go語(yǔ)言如何將整型轉(zhuǎn)為字符串

3、strconv.FormatInt

支持 int64 類(lèi)型轉(zhuǎn)換成字符串
參數(shù) i 是要被轉(zhuǎn)換的整數(shù), base 是進(jìn)制,例如2進(jìn)制,支持2到36進(jìn)制。

//FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters ‘a(chǎn)' to ‘z' for digit values >= 10.
func FormatInt(i int64, base int) string

使用示例:

str := strconv.FormatInt(a, 10)

擴(kuò)展知識(shí):字符串轉(zhuǎn)int整數(shù)

1、strconv.Atoi

比較常見(jiàn)的方法

// Atoi returns the result of ParseInt(s, 10, 0) converted to type int.
func Atoi(s string) (int, error)

使用示例:

i,err := strconv.Atoi(a)

2、strconv.ParseInt

功能非常強(qiáng)大

// ParseInt interprets a string s in the given base (0, 2 to 36) and
// bit size (0 to 64) and returns the corresponding value i.
func ParseInt(s string, base int, bitSize int) (i int64, err error)

  • 參數(shù)1 數(shù)字的字符串形式

  • 參數(shù)2 數(shù)字字符串的進(jìn)制 比如二進(jìn)制 八進(jìn)制 十進(jìn)制 十六進(jìn)制

  • 參數(shù)3 返回結(jié)果的bit大小 也就是int8 int16 int32 int64

使用示例:

i, err := strconv.ParseInt("123", 10, 32)

“go語(yǔ)言如何將整型轉(zhuǎn)為字符串”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)頁(yè)名稱(chēng):go語(yǔ)言如何將整型轉(zhuǎn)為字符串
轉(zhuǎn)載源于:http://bm7419.com/article44/pcdgee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化域名注冊(cè)、做網(wǎng)站App設(shè)計(jì)、網(wǎng)站收錄云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)