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.