Golang 将双精度类型变量转换为字符串
在本教程中,我们将学习如何使用Golang编程语言将双精度(float64)类型变量转换为字符串变量。
双精度 代表双精度浮点数。在编程语言中,双精度数据类型用于更精确地处理小数,即使用双精度数据类型,我们可以在小数点后存储更多位数,并保持准确性。
字符串 数据类型用于存储字符序列。它可以是字面量或字母。字符串变量的大小为1字节或8位。
示例1:使用Itoa()函数将双精度类型变量转换为字符串的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的双精度浮点型变量并赋值。
-
打印双精度浮点型变量的类型和值。
-
使用FormatFloat()函数将双精度浮点型值转换为字符串。
-
接下来,使用Printf()函数打印转换后的整型值以及数据类型。
-
在接下来的示例中,重新分配不同的双精度浮点型变量值并将其转换为字符串并打印结果。
示例2:使用Sprintf()函数将双精度浮点型变量转换为字符串的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()函数,这个函数是可执行程序的入口点。它不接受任何参数,也不返回任何东西。
-
现在声明并初始化一个双精度值到一个变量中,之后我们将把它转换为字符串。
-
打印该双精度变量的类型和值。
-
使用Sprintf()函数将双精度类型的数据转换为字符串类型的值。
-
将得到的输出存储在一个名为result的单独变量中。
-
接下来,我们使用Printf()函数打印转换后的整数值以及数据类型。
-
您可以在即将到来的示例中重新分配不同的双精度值并将其转换为字符串。
结论
我们已成功编译并执行了一个Go语言程序,用于将双精度类型变量转换为字符串,并附带示例。