Home > Article > Backend Development > Convert floating point number to string using strconv.FormatFloat function
Use the strconv.FormatFloat function to convert floating point numbers into strings
In the Go language, we often need to convert floating point numbers into string types for output or storage needs. The strconv package is provided in the Go language, and the FormatFloat function in it can convert floating point numbers into string types.
The FormatFloat function has three parameters: f represents the floating point number to be converted, fmt represents the format, and prec represents the number of decimal places to be retained. Among them, the f parameter must be float64 type data.
The following is a simple sample code that demonstrates how to use the FormatFloat function to convert a floating point number to a string.
package main import ( "fmt" "strconv" ) func main() { // 将浮点数转换为字符串,保留2位小数 val := 3.1415926 str := strconv.FormatFloat(val, 'f', 2, 64) fmt.Println("浮点数转换为字符串:", str) }
In the above code, we define a floating point variable val and assign it a value of 3.1415926. Then, we call the strconv.FormatFloat function to convert val to a string type, retaining 2 decimal places, and assign the converted string to the variable str. Finally, we use the fmt.Println function to print out str.
Run the above code, the output result is as follows:
浮点数转换为字符串: 3.14
As you can see, the floating point number 3.1415926 is successfully converted to the string type, and 2 decimal places are retained.
In addition to converting to ordinary strings, we can also convert floating point numbers to strings represented in scientific notation. The following is a sample code:
package main import ( "fmt" "strconv" ) func main() { // 将浮点数转换为科学计数法字符串 val := 123456.789 str := strconv.FormatFloat(val, 'e', -1, 64) fmt.Println("浮点数转换为科学计数法字符串:", str) }
In the above code, we assign the value of val to 123456.789 and call the strconv.FormatFloat function to convert it to a string type represented in scientific notation. Among them, the value of the fmt parameter is 'e', which means using scientific notation to represent the string. Running the above code will get the following output:
浮点数转换为科学计数法字符串: 1.23456789e+05
As you can see, the floating point number 123456.789 is successfully converted to a string type represented by scientific notation.
Through the above sample code, we learned how to use the strconv.FormatFloat function to convert floating point numbers to string types. According to actual needs, we can control the accuracy and format of conversion by adjusting parameters. The FormatFloat function in the strconv package is a very practical function in the Go language. It can be very helpful in handling the conversion problem between floating point numbers and strings.
The above is the detailed content of Convert floating point number to string using strconv.FormatFloat function. For more information, please follow other related articles on the PHP Chinese website!