Golang程序 将double变量转换为字符串
在本教程中,我们将学习如何使用Golang编程语言将双倍(float64)类型的变量转换为字符串变量。
double 代表的是双精度。在编程语言中,双倍数据类型被用来更精确地处理小数,即使用双倍数据类型,我们可以准确地存储小数点后的更多数字。
字符串 数据类型用于存储一连串的字符。它可以是字数或字母的形式。字符串变量的大小为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()函数打印转换后的int值以及数据类型。
-
在接下来的例子中,重新分配不同的双倍变量值并将其转换为字符串,然后打印结果。
例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()函数,将转换后的int值与数据类型一起打印出来。
-
在接下来的例子中,你可以重新分配不同的双倍值并将其转换为字符串。
总结
我们已经成功地编译并执行了一个Go语言程序,将双倍类型的变量转换为字符串,并附有实例。