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
Related:
Golang JSON Encode Decode example
Tags: Golang tutorial, Convert Float to String, String to float,
Article Contributed By :
|
|
|
|
714 Views |