Golang程序 将双倍类型的变量转换为int类型的变量

Golang程序 将双倍类型的变量转换为int类型的变量

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

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

例子1:Go语言代码将双倍类型的变量转换为int

语法

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

为了将浮点类型的输入值转换为整数值,我们可以简单地将int()包裹在浮点类型的变量上。

尽管Go可以将双数转换为整数,但应用程序将失去浮点数的精度。当你用int()或其独立于架构的数据类型之一包裹double时,它的功能类似于在其他整数类型之间的转换。

例子

// 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”。使用int()函数将浮点类型的输入转换为整数值。

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

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

例2:使用自定义函数将双数转换为英数

Golang的数据类型是非常严格的,因此如果你声明一个变量的类型为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程序代码,在上述两个例子中,将双数类型的变量转换为整数类型的变量。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程