Go語言程式:將double型別變數轉換為字串


在本教程中,我們將學習如何使用 Go 程式語言將 double (float64) 型別變數轉換為字串變數。

Double 代表雙精度。在程式語言中,double 資料型別用於更精確地處理十進位制數,即使用 double 資料型別,我們可以更精確地儲存小數點後的更多位數。

String 資料型別用於儲存字元序列。它可以是文字或字母的形式。字串變數的大小為 1 位元組或 8 位。

示例 1:使用 Itoa() 函式將 double 型別變數轉換為字串的 GO 語言程式碼。

語法

func FormatFloat(x float, fmt byte, prec, bitsize int) string

FormatFloat - 此函式用於將十進位制數轉換為字串。它接受四個引數:第一個是我們希望轉換的浮點數;fmt 和 prec 用於控制輸出;最後一個引數是整數結果的位數,可以是 32 位或 64 位。

示例

package main import ( "fmt" "strconv" ) // fmt package provides the function to print anything // Package strconv allows us to use other predefined methods like FormatFloat() func main() { // initialize a number variable and asign float type value to it var number float64 = 3.14159 // printing the decimal type value fmt.Printf("Type of variable before conversion : %T \nValue : %v\n", number, number) // converting double to string output := strconv.FormatFloat(number, 'g', 5, 64) // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", output, output) // print a new line fmt.Println() // reasign float type value to it number = -285.32 // printing the decimal type value fmt.Printf("Type of variable before conversion : %T \nValue : %v\n", number, number) // converting double to string output = strconv.FormatFloat(number, 'g', 5, 64) // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", output, output) }

輸出

Type of variable before conversion : float64
Value : 3.14159
Type of variable after conversion : string
Value : 3.1416
Type of variable before conversion : float64
Value : -285.32
Type of variable after conversion : string
Value : -285.32

程式碼描述

  • 在上面的程式中,我們首先宣告包 main。

  • 我們匯入了 fmt 包,其中包含 fmt 包的檔案,允許我們在螢幕上列印任何內容。

  • 接下來,我們啟動函式 main()。

  • 現在宣告並初始化一個名為 number 的 double 型別變數,併為其賦值。

  • 列印 double 型別變數的型別和值。

  • 使用 FormatFloat() 函式將 double 型別值轉換為字串。

  • 接下來,我們使用 Printf() 函式列印轉換後的 int 值及其資料型別。

  • 在接下來的示例中,重新分配 double 變數的不同值,將其轉換為字串,並列印結果。

示例 2:使用 Sprintf() 函式將 double 型別變數轉換為字串的 GO 語言程式碼。

語法

func Sprintf(format string, a ...interface{}) string

Sprintf() 函式返回格式化的字串。第一個引數應該是字串格式,後面跟著可變數量的引數。然後,此函式將結果作為格式化的字串格式返回。

示例

// Golang program to convert Int data type to Float using Sprintf() function package main // fmt package provides the function to print anything import ( "fmt" ) // start the main function this is the main starting point of the program func main() { // declare the variable and assign a value to it var number float64 = 273.54 // printing the float type value fmt.Printf("Type of variable before conversion : %T \nValue : %v\n", number, number) // use the Sprintf() method on the the variable to convert it to string and store the // result in a seperate variable. output := fmt.Sprintf("%v", number) // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", output, output) // printing a new line fmt.Println() // reassign a value to it number = -73.54 // printing the float type value fmt.Printf("Type of variable before conversion : %T \nValue : %v\n", number, number) // use the Sprintf() method on the the variable to convert it to string and store the // result in a seperate variable. output = fmt.Sprintf("%v", number) // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", output, output) }

輸出

Type of variable before conversion : float64
Value : 273.54
Type of variable after conversion : string
Value : 273.54
Type of variable before conversion : float64
Value : -73.54
Type of variable after conversion : string
Value : -73.54

程式碼描述

  • 在上面的程式中,我們首先宣告包 main。

  • 我們匯入了 fmt 包,其中包含 fmt 包的檔案,允許我們在螢幕上列印任何內容。

  • 接下來,我們啟動函式 main(),此函式是可執行程式的入口點。它不接受任何引數也不返回任何內容。

  • 現在將雙精度值宣告並初始化為一個變數,稍後我們將將其轉換為字串。

  • 列印該 double 變數的型別和值。

  • 使用 Sprintf() 函式將 double 型別資料轉換為字串型別值。

  • 將獲得的輸出儲存在一個名為 result 的單獨變數中。

  • 接下來,我們使用 Printf() 函式列印轉換後的 int 值及其資料型別。

  • 您可以在接下來的示例中重新分配 double 的不同值並將其轉換為字串。

結論

我們已成功編譯並執行了一個 Go 語言程式,用於將 double 型別變數轉換為字串,並附帶示例。

更新於:2022年11月22日

715 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告