Go語言程式將double型別變數轉換為int


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

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

示例 1:將 DOUBLE 型別變數轉換為 INT 的 GO 語言程式碼

語法

fmt.Println(int(b))
Int(x)                    

要將浮點型別輸入值轉換為整數值,我們可以簡單地將 int() 包裹在浮點型別變數周圍。

儘管 Go 可以將 double 轉換為整數,但應用程式會丟失浮點數的精度。當您使用它在其他整數型別之間進行轉換時,它具有類似的功能,當您將 double 包裹在 int() 或其架構無關的資料型別中時。

示例

// Golang program to convert Int data type to Float package main // fmt package provides the function to print anything import ( "fmt" "reflect" ) // 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 var typeof = reflect.TypeOf(number) // use the int() function to convert float to int and store the output in a variable called result. var result int = int(number) var typeof2 = reflect.TypeOf(result) // printing the float type value fmt.Println("The float value =", number,"\nType of variable=",typeof) // print the int value obtained after the conversion fmt.Printf("The integer value after conversion = %d\n", result) fmt.Println("Variable type =",typeof2) // reasigning the number value }

輸出

The float value = 273.54
Type of variable= float64
The integer value after conversion = 273
Variable type = int                    

Go 語言透過去除小數點來完成上述轉換。它省略了小數點後的所有數字。請注意,它不會將數字四捨五入到下一個或前一個數字。如果您希望將結果四捨五入到下一個數字,則應使用其他數學函式,如 round()。

程式碼描述

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

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

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

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

  • 接下來,宣告一個整數變數“result”。使用 int() 函式圍繞浮點型別輸入將其轉換為整數值。

  • 將結果儲存在一個名為 result 的單獨變數中。

  • 接下來,我們使用 Printf() 函式列印轉換後的 int 值。

示例 2:使用自定義函式將 DOUBLE 轉換為 INT

Golang 的資料型別非常嚴格,因此,如果您將變數的型別宣告為 float64,則只能輸入浮點數作為值。如果您嘗試將整數寫入指定為 float 型別的變數,則只會看到錯誤。

因此,您需要將變數的 double 型別轉換為整數。請參閱以下示例,瞭解如何手動執行此任務。

示例

// Golang program to convert Int data type to Float package main // fmt package provides the function to print anything import ( "fmt" "reflect" ) func floatToint(value float64) int { // conversion code return int(value * 1) } // 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 var typeof = reflect.TypeOf(number) // call the floatToint function to convert float value to int and store the output in a // variable called result. var result int = floatToint(number) // printing the float type value fmt.Printf("The float value = %.2f\n", number) fmt.Println("Variable Type =",typeof) // print the int value obtained after the conversion fmt.Printf("The integer value after conversion = %d\nVaraibel Type = %T\n", result, result) // printing a new line fmt.Println() // reassigining the number variable }

輸出

The float value = 273.54
Variable Type = float64
The integer value after conversion = 273
Varaibel Type = int

程式碼描述

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

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

  • 定義一個名為 floatToint() 的函式,其中包含執行所需轉換的程式碼行。

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

  • 現在宣告並初始化一個浮點值到一個變數,我們稍後將其轉換為整數型別變數。

  • 呼叫 floatToint() 函式並將上述初始化的浮點值作為引數傳遞給它。

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

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

結論

在以上兩個示例中,我們已成功編譯並執行了 Go 語言程式程式碼,用於將 double 型別變數轉換為整數型別變數。

更新於: 2022-11-22

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.