go語言實作string轉float的方法
go語言實作string轉float的方法:1、建立go範例檔;2、定義一個字串;3、透過「v1, err:=strconv.ParseFloat(v, 32)」方式將string轉float即可。
本文環境:windows10系統、Go1.14版,Dell G3電腦本文適用於所有品牌的電腦。
Go語言string,int,int64 ,float之間型別轉換方法
Go語言中int型別與string型別都是屬於基本資料型別,兩種類型的轉換都非常簡單。以下透過本文給大家分享Go語言string,int,int64 ,float之間型別轉換方法,有興趣的朋友一起看看吧
(1)int轉string
s := strconv.Itoa(i) 等价于s := strconv.FormatInt(int64(i), 10)
(2 )int64轉string
i := int64(123) s := strconv.FormatInt(i, 10)
第二個參數為基數,可選2~36
註:對於無符號整形,可以使用FormatUint(i uint64, base int)
(3)string轉int
i, err := strconv.Atoi(s)
(4)string轉int64
i, err := strconv.ParseInt(s, 10, 64)
第二個參數為基數(2~36),第三個參數位元大小表示期望轉換的結果類型,其值可以為0, 8, 16, 32和64,分別對應int, int8, int16, int32和int64
(5)float相關
float轉string:
v := 3.1415926535 s1 := strconv.FormatFloat(v, 'E', -1, 32)//float32s2 := strconv.FormatFloat(v, 'E', -1, 64)//float64
函數原型及參數意義可參考:https://golang.org/pkg/strconv/#FormatFloat
string轉float:
s := "3.1415926535" v1, err := strconv.ParseFloat(v, 32) v2, err := strconv.ParseFloat(v, 64)
PS:go語言string、int、int64互相轉換
//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到string string := 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語言》
以上是go語言實作string轉float的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

Go語言中結構體定義的兩種方式:var與type關鍵字的差異Go語言在定義結構體時,經常會看到兩種不同的寫法:一�...

Go語言中哪些庫是大公司開發或知名開源項目?在使用Go語言進行編程時,開發者常常會遇到一些常見的需求,�...

Go編程中的資源管理:Mysql和Redis的連接與釋放在學習Go編程過程中,如何正確管理資源,特別是與數據庫和緩存�...
