Golang Example - Convert(Cast) String to Float and Float to String


This post covers how to convert(cast) String type to float type or float type to String with an example.

Golang strconv package

The string is a group of characters referenced by a variable. A float is a numeric number that stores set of all IEEE-754 64-bit floating-point numbers. Both are of different types, Automatic conversions will not occur. The developer needs to write a code to handle this. Golang provides standard inbuilt package strconv to do string conversions to other types. It is a common task of the developer to convert String to float or float to String.

Convert String to float example

strconv package provides ParseFloat function to convert to float numeric types. You can check more about this function here

Here is syntax

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

arguments are
The string is an input string. if given string is not numeric, It gives error strconv.ParseFloat: parsing “abc”: invalid syntax
bisize is of int type with values 32 or 64.
Here is a strconv  ParseFloat function example program

package main  
  
import (  
 "fmt"  
 "reflect"  
 "strconv"  
)  
  
func main() {  
 floatString := "587.23"  
 floatNumb, err := strconv.ParseFloat("abc", 32)  
 fmt.Println(floatNumb)  
 fmt.Println(reflect.TypeOf(floatNumb))  
 fmt.Println(err)  
 if n1, err := strconv.ParseFloat(floatString, 32); err == nil {  
  fmt.Printf("%T, %v\n", n1, n1)  
 }  
 if n2, err := strconv.ParseFloat(floatString, 64); err == nil {  
  fmt.Printf("%T, %v\n", n2, n2)  
 }  
  
}  

An output of the above program

0  
float64  
strconv.ParseFloat: parsing "abc": invalid syntax  
float64, 587.22998046875  
float64, 587.23  

Convert float number to String example

There are two ways to convert float type to String type

one is using fmt.Sprintf() function, other is using strconv.FormatFloat function

fmt Sprintf function example

fmt package provides Sprintf is used to convert to string

Here is a syntax

func Sprintf(format string, a ...interface{}) string 

the format is a string
%f - Decimal format
%e - scientific notation format
%.5f- the precision with 5 format
%g - exponent format

and parameter a is of the vardiac interface which accepts any type of value

Here is a fmt Sprintf function example program

package main  
  
import (  
 "fmt"  
 "reflect"  
)  
  
func main() {  
 str := fmt.Sprintf("%f", 45.1) //45.100000  
 fmt.Println(reflect.TypeOf(str))  
 fmt.Println(str)  
 str1 := fmt.Sprintf("%e", 45.1) //4.510000e+01  
 fmt.Println(reflect.TypeOf(str1))  
 fmt.Println(str1)  
 str2 := fmt.Sprintf("%.5f", 45.1) //45.10000  
 fmt.Println(reflect.TypeOf(str2))  
 fmt.Println(str2)  
 str3 := fmt.Sprintf("%g", 45.1) //45.1  
 fmt.Println(reflect.TypeOf(str3))  
 fmt.Println(str3)  
}  

Output is

string  
45.100000  
string  
4.510000e+01  
string  
45.10000  
string  
45.1  

strconv FormatFloat function example 

strconv package has FormatFloat function is to convert the floating number to string with a given format and precision Here is a syntax of this function

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

Arguments are

floating number converted to a string
fmt is of byte contains values b -binary exponent, e or E - Decimal exponent, F - no exponent, g or G - large exponents
prec  is of int precision value
bitSize is of int type which contains 32 and 64 values Here is an strconv FormatFloat function example program

package main  
  
import (  
 "fmt"  
 "reflect"  
 "strconv"  
)  
  
func main() {  
 floatNumb := 5.789455  
 str := strconv.FormatFloat(floatNumb, 'f', 5, 64)  
 fmt.Println(reflect.TypeOf(str))  
 fmt.Println(str)  
 str1 := strconv.FormatFloat(floatNumb, 'e', 5, 64)  
 fmt.Println(reflect.TypeOf(str1))  
 fmt.Println(str1)  
 str2 := strconv.FormatFloat(floatNumb, 'g', 5, 64)  
 fmt.Println(reflect.TypeOf(str2))  
 fmt.Println(str2)  
}  
  

Output is

string  
5.78946  
string  
5.78946e+00  
string  
5.7895 
Similar Posts