Convert between Float and String in Golang

Convert between float and string in golang

In the previous blog, we learnt about int to string conversion and string to int in Golang. This time we are going to learn how we can convert between float and String in Golang.

learn the above conversions from here.

Convert Float to String in Golang

func FormatFloat(f float64, b byte, prec, bitSize int) string

The Go strconv FormatFloat function takes a float value, the format byte, precision, and the bit size and returns the string version of float value.

Example:

v := 3.1415926535

var s string

s = strconv.FormatFloat(v, 'E', -1, 32)
fmt.Printf("%T, %v\n", s, s)

s = strconv.FormatFloat(v, 'E', -1, 64)
fmt.Printf("%T, %v\n", s, s)
string, 3.1415927E+00
string, 3.1415926535E+00

Second argument taken by strconv FormatFloat is format byte.

  • b‘ for (-ddddp±ddd, a binary exponent)
  • e‘ for (-d.dddde±dd, a decimal exponent)
  • E‘ for (-d.ddddE±dd, a decimal exponent)
  • f‘ for (-ddd.dddd, no exponent)
  • g‘ for (‘e’ for large exponents, ‘f’ otherwise)
  • G‘ (‘E’ for large exponents, ‘f’ otherwise)
  • x‘ (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent)
  • X‘ (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent)

Example:

v := 3.1415926535

var s string

s = strconv.FormatFloat(v, 'b', -1, 64)
fmt.Printf("%T, %v\n", s, s)

s = strconv.FormatFloat(v, 'e', -1, 32)
fmt.Printf("%T, %v\n", s, s)

s = strconv.FormatFloat(v, 'E', -1, 32)
fmt.Printf("%T, %v\n", s, s)

s = strconv.FormatFloat(v, 'f', -1, 32)
fmt.Printf("%T, %v\n", s, s)

s = strconv.FormatFloat(v, 'G', -1, 32)
fmt.Printf("%T, %v\n", s, s)

s = strconv.FormatFloat(v, 'x', -1, 32)
fmt.Printf("%T, %v\n", s, s)

s = strconv.FormatFloat(v, 'X', -1, 32)
fmt.Printf("%T, %v\n", s, s)
string, 7074237751826244p-51
string, 3.1415927e+00
string, 3.1415927E+00
string, 3.1415927
string, 3.1415927
string, 0x1.921fb6p+01
string, 0X1.921FB6P+01

Convert String to Float in Golang

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

The Go strconv ParseFloat function takes a string and the bit size in which the float value to be converted and returns the float value in float64 and an error if parsing fails.

ParseFloat function accepts decimal and hexadecimal floating-point number.

Example:

var n float64
var err error

s := "3.1415926535"

n, err = strconv.ParseFloat(s, 32)
fmt.Printf("%T %v %v\n", n, n, err)

n, err = strconv.ParseFloat(s, 64)
fmt.Printf("%T %v %v\n", n, n, err)

n, err = strconv.ParseFloat("nan", 32)
fmt.Printf("%T %v %v\n", n, n, err)

n, err = strconv.ParseFloat("inf", 32)
fmt.Printf("%T %v %v\n", n, n, err)

n, err = strconv.ParseFloat("inf", 32)
fmt.Printf("%T %v %v\n", n, n, err)

n, err = strconv.ParseFloat("+Inf", 32)
fmt.Printf("%T %v %v\n", n, n, err)

n, err = strconv.ParseFloat("-Inf", 32)
fmt.Printf("%T %v %v\n", n, n, err)

n, err = strconv.ParseFloat("-0", 32)
fmt.Printf("%T %v %v\n", n, n, err)

n, err = strconv.ParseFloat("+0", 32)
fmt.Printf("%T %v %v\n", n, n, err)
float64 3.1415927410125732 <nil>
float64 3.1415926535 <nil>
float64 NaN <nil>
float64 +Inf <nil>
float64 +Inf <nil>
float64 +Inf <nil>
float64 -Inf <nil>
float64 -0 <nil>
float64 0 <nil>

Learn more about Golang strconv package from the official Documentation.

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *