Welcome To Golang By Example

Parse a string to float in Go (Golang)

strconv.ParseFloat() function can be used to parse a string representation of a float

https://golang.org/pkg/strconv/#ParseFloat

Below is the signature of the function

func ParseFloat(s string, bitSize int) (float64, error) 


Some points worth noting

Let’s see a working code

package main

import (
    "fmt"
    "strconv"
)

func main() {
    e1 := "1.3434"
    if s, err := strconv.ParseFloat(e1, 32); err == nil {
        fmt.Printf("%T, %v\n", s, s)
    }
    if s, err := strconv.ParseFloat(e1, 64); err == nil {
        fmt.Printf("%T, %v\n", s, s)
    }
}

Output

float64, 1.343400001525879
float64, 1.3434