Golang 将double类型变量转换为int类型

Golang 将double类型变量转换为int类型

在本教程中,我们将学习如何使用Golang编程语言将double类型变量转换为整型变量。

Double 代表双精度。在编程语言中,双精度数据类型用于更精确地处理小数,即使用双精度数据类型,我们可以在小数点后存储更多数字,且具有更高的精度。

示例1:将double类型变量转换为int类型的Go语言代码:

语法

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

将浮点类型的输入值转换为整数值,我们只需将int()包装在浮点类型变量周围。

尽管Go可以将双精度浮点数转换为整数,但应用程序将丢失浮点数的精度。当您将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(),这个函数是可执行程序的入口点。它不接受任何参数,也不返回任何内容。

  • 现在声明并初始化一个双精度类型的变量number,并赋值给它。

  • 接下来,声明一个整数变量”result”。将 float 类型的输入包裹在 int() 函数中,将其转换为整数值。

  • 将结果存储在一个单独的变量中,命名为result。

  • 接下来,我们使用Printf()函数打印转换后的整数值。

示例2:使用自定义函数将双精度数转换为整数

Go语言的数据类型非常严格,因此如果您将变量的类型声明为float64,则只能输入浮点数作为值。如果您尝试将整数数字写入指定为float类型的变量中,您将只会看到错误。

因此,您需要将双精度类型的变量转换为整数。请查看下面的示例,了解如何手动执行此操作。

示例

// 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值以及数据类型。

结论

我们已经成功编译并执行了上述两个示例中将双精度类型变量转换为整数类型变量的Golang程序代码。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程