How to convert String to Float in golang
In this example we will covert string to float by using Syntax Example to Convert String to Float i Golang Output Convert Float to String Output: Related: Golang JSON Encode Decode example Tags: Golang tutorial, Convert Float to String, String to float, strconv.ParseFloat
function. This ParseFloat() takes two arguments one is float string and second is bitSize32 or 64
func ParseFloat(s string, bitSize int) (float64, error)
package main
import (
"fmt"
"strconv"
)
func main() {
f := "9.12345"
if s, err := strconv.ParseFloat(f, 32); err == nil {
fmt.Println(s)
}
if s, err := strconv.ParseFloat(f, 64); err == nil {
fmt.Println(s)
}
}
9.12345027923584
9.12345
package main
import (
"fmt"
)
func main() {
f := 9.12345
s := fmt.Sprintf("%f",f)
fmt.Println(s)
}
9.123450
30 Views