how to convert string to float in golang?
September 21, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
To convert string to float in golang, just use ParseFloat()
method and pass your string, it will convert string into float.
Follow the below tutorial if you are struggling with installing GO in windows.
https://aguidehub.com/blog/how-to-install-golang-in-windows/
Today, I will show you how do I convert string to float in golang, as above mentioned I’m going to use ParseFloat()
way.
The strconv
package provide ParseFloat()
method.
Let’s start our Golang convert string to float example
Convert whole string into float example
main.go
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
s := "7.86"
f, err := strconv.ParseFloat(s, 8)
fmt.Println(f, err, reflect.TypeOf(f))
}
In the above example, we have converted string to float and printed in golang console. let’s check the output.
Output
7.86 <nil> float64
I hope it helps you, All the best 👍.