Trending Articles       10 Flutter Listview Examples       Flutter Custom Button Example       Flutter Audio Recording       Flutter 2.0 Autocomplete TextField       Current Location Google Maps

How to convert String to Float in golang

In this example we will covert string to float by using strconv.ParseFloat function. This ParseFloat() takes two arguments one is float string and second is bitSize32 or 64

Syntax

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

 

Example to Convert String to Float i Golang

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)
    }

}

 

Output

9.12345027923584
9.12345

 

Convert Float to String

package main

import (
"fmt"

)

func main() {
    f := 9.12345
    s := fmt.Sprintf("%f",f)

    fmt.Println(s)



}

 

Output:

9.123450

 

Execute at golang playgroung

 

Related:

Golang JSON Encode Decode example

Tags: Golang tutorial, Convert Float to String, String to float,


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

121 Views

Flutter Questions

Android Questions