Go – How to convert string into float number in go

Here, We will seeÂ how to convert string into float number in go.Â We can do it by using ParseFloat()Â function in strconv package in go golang.

ParseFloat() function converts the string into a floating-point number with the precision specified by bitSize that could be 32 for float32 and 64 for float64.

ParseFloat() function accepts decimal and hexadecimal floating-point number as input parameter.

Function prototype:

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

Input Parameter:

s: s is a floating point number in string
format.```

Return value:

```ParseFloat() function in strconv package returns
floating-point number.```

Example with code:

```package main

import (

"fmt"
"strconv"
"log"

)

func main() {

no := "5.1245333"

fno, err := strconv.ParseFloat(no, 32)

if err != nil {

log.Fatal(err)
return

}

fmt.Printf("%T  %v\n", fno, fno)

fno, err = strconv.ParseFloat(no, 64)

if err != nil {

log.Fatal(err)
return

}

fmt.Printf("%T  %v\n", fno, fno)

fno, err = strconv.ParseFloat("NaN", 32)

if err != nil {

log.Fatal(err)
return

}

fmt.Printf("%T  %v\n", fno, fno)

fno, err = strconv.ParseFloat("nan", 32)

if err != nil {

log.Fatal(err)
return

}

fmt.Printf("%T  %v\n", fno, fno)

fno, err = strconv.ParseFloat("inf", 32)

if err != nil {

log.Fatal(err)
return

}

fmt.Printf("%T  %v\n", fno, fno)

fno, err = strconv.ParseFloat("+Inf", 32)

if err != nil {

log.Fatal(err)
return

}

fmt.Printf("%T  %v\n", fno, fno)

fno, err = strconv.ParseFloat("-Inf", 32)

if err != nil {

log.Fatal(err)
return

}

fmt.Printf("%T  %v\n", fno, fno)

fno, err = strconv.ParseFloat("-0", 32)

if err != nil {

log.Fatal(err)
return

}

fmt.Printf("%T  %v\n", fno, fno)

fno, err = strconv.ParseFloat("+0", 32)

if err != nil {

log.Fatal(err)
return

}

fmt.Printf("%T  %v\n", fno, fno)

}
```

Output:

```float64 5.124533176422119

float64 5.1245333

float64 NaN

float64 NaN

float64 +Inf

float64 +Inf

float64 -Inf

float64 -0

float64 0```

`https://www.techieindoor.com/go-lang-tutorial/`
`https://golang.org/doc/https://golang.org/pkg/`