To convert a string to float in Go, you can use the 'strconv.ParseFloat()' function.
In this example,
str1
with a value of "3.14"
.strconv.ParseFloat()
function to convert the string to a float, specifying the bit size (64 for float64) and store the result in a variable float1
.float1
.// Convert "3.14" String to Float
package main
import (
"fmt"
"strconv"
)
func main() {
str1 := "3.14"
float1, _ := strconv.ParseFloat(str1, 64)
fmt.Printf("Float value is: %f", float1)
}
Float value is: 3.140000
In this example,
str2
with a value of "10.5"
.strconv.ParseFloat()
function to convert the string to a float, specifying the bit size (64 for float64) and store the result in a variable float2
.float2
.// Convert "10.5" String to Float
package main
import (
"fmt"
"strconv"
)
func main() {
str2 := "10.5"
float2, _ := strconv.ParseFloat(str2, 64)
fmt.Printf("Float value is: %f", float2)
}
Float value is: 10.500000
In this tutorial, we learned How to convert String to Float in Go language with well detailed examples.