Go語言string,int,int64 ,float之間類型轉換方法

來源:互聯網
上載者:User

標籤:tin   targe   printing   body   highlight   字元   except   無符號   org   

(1)int轉string

?
12 s := strconv.Itoa(i)等價於s := strconv.FormatInt(int64(i), 10)

(2)int64轉string

?
12 i := int64(123)s := strconv.FormatInt(i, 10)

第二個參數為基數,可選2~36

註:對於無符號整形,可以使用FormatUint(i uint64, base int)

(3)string轉int

?
1 i, err := strconv.Atoi(s)

(4)string轉int64

?
1 i, err := strconv.ParseInt(s, 10, 64)

第二個參數為基數(2~36),第三個參數位大小表示期望轉換的結果類型,其值可以為0, 8, 16, 32和64,分別對應 int, int8, int16, int32和int64

(5)float相關

float轉string:

?
12 v := 3.1415926535s1 := strconv.FormatFloat(v, ‘E‘, -1, 32)//float32s2 := strconv.FormatFloat(v, ‘E‘, -1, 64)//float64

函數原型及參數含義具體可查看:https://golang.org/pkg/strconv/#FormatFloat

string轉float:

?
123 s := "3.1415926535"v1, err := strconv.ParseFloat(v, 32)v2, err := strconv.ParseFloat(v, 64)

 PS:go語言string、int、int64互相轉換

?
12345678910111213141516171819 //string到int int,err:=strconv.Atoi(string) //string到int64 int64, err := strconv.ParseInt(string, 10, 64) //int到string string:=strconv.Itoa(int) //int64到string string:=strconv.FormatInt(int64,10)//string到float32(float64)float,err := strconv.ParseFloat(string,32/64)//float到stringstring := strconv.FormatFloat(float32, ‘E‘, -1, 32)string := strconv.FormatFloat(float64, ‘E‘, -1, 64)// ‘b‘ (-ddddp±ddd,二進位指數)// ‘e‘ (-d.dddde±dd,十進位指數)// ‘E‘ (-d.ddddE±dd,十進位指數)// ‘f‘ (-ddd.dddd,沒有指數)// ‘g‘ (‘e‘:大指數,‘f‘:其它情況)// ‘G‘ (‘E‘:大指數,‘f‘:其它情況)

總結

以上所述是小編給大家介紹的Go語言string,int,int64 ,float之間類型轉換方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對指令碼之家網站的支援!

您可能感興趣的文章:
  • mongodb錯誤tcmalloc: large alloc out of memory, printing stack and exiting解決辦法
  • mongodb exception: $concat only supports strings, not NumberInt32解決辦法
  • Go語言interface詳解
  • go語言中的interface使用執行個體
  • golang實現unicode轉換為字串string的方法

原文連結:http://www.cnblogs.com/vdvvdd/archive/2017/07/20/7211122.html

如對本文有疑問,請提交到交流社區,廣大熱心網友會為你解答!! 點擊進入社區

Go語言string,int,int64 ,float之間類型轉換方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.