To convert a float to a string in Go, you can use the strconv
package's FormatFloat
function.
In this example,
float1
with a value of 3.14
.strconv
package.FormatFloat
function to convert the float to a string and store the result in a variable str1
.str1
.package main
import (
"fmt"
"strconv"
)
func main() {
float1 := 3.14
str1 := strconv.FormatFloat(float1, 'f', -1, 64)
fmt.Println("String value is:", str1)
}
String value is: 3.14
In this example,
float2
with a value of 10.5
.strconv
package.FormatFloat
function to convert the float to a string and store the result in a variable str2
.str2
.package main
import (
"fmt"
"strconv"
)
func main() {
float2 := 10.5
str2 := strconv.FormatFloat(float2, 'f', -1, 64)
fmt.Println("String value is:", str2)
}
String value is: 10.5
In this tutorial, we learned How to convert Float to String in Go language with well detailed examples.