當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang strconv.FormatUint()用法及代碼示例

Go 語言提供內置支持,以通過 strconv 包實現與基本數據類型的字符串表示之間的轉換。該包提供了一個 FormatUint() 函數,用於返回給定基數中 x 的字符串表示,即 2 <= base <= 36。 這裏,結果使用小寫字母 ‘a’ 到 ‘z’ 表示較大的數字值比等於 10。要訪問 FormatUint() 函數,您需要在程序中借助 import 關鍵字導入 strconv 包。

用法:

func FormatUint(x uint64, base int) string

參數:這個函數有兩個參數,即 x 和 base。

返回值:此函數返回給定基數中 x 的字符串表示,即 2 <= base <= 36。

範例1:




// Golang program to illustrate
// strconv.FormatUint() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
    // Finding the string representation
    // of given value in the given base
    // Using FormatUint() function
    fmt.Println(strconv.FormatUint(11, 2))
    fmt.Println(strconv.FormatUint(24, 10))
  
}

輸出:

1011
24

範例2:


// Golang program to illustrate
// strconv.FormatUint() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
    // Finding the string representation
    // of given value in the given base
    // Using FormatUint() function
    val1:= uint64(25)
    res1:= strconv.FormatUint(val1, 2)
    fmt.Printf("Result 1:%v", res1)
    fmt.Printf("\nType 1:%T", res1)
  
    val2:= uint64(20)
    res2:= strconv.FormatUint(val2, 16)
    fmt.Printf("\nResult 2:%v", res2)
    fmt.Printf("\nType 2:%T", res2)
  
}

輸出:

Result 1:11001
Type 1:string
Result 2:14
Type 2:string



相關用法


注:本文由純淨天空篩選整理自ankita_saini大神的英文原創作品 How to use strconv.FormatUint() Function in Golang?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。