Home > Backend Development > Golang > Convert floating point number to string using strconv.FormatFloat function

Convert floating point number to string using strconv.FormatFloat function

WBOY
Release: 2023-07-25 11:45:12
Original
1307 people have browsed it

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)
}
Copy after login

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
Copy after login

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)
}
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template