{

Golang Example - Convert(Cast) String to/from Float


Golang example Convert(Cast) String to/from Float

This post covers converting (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 a set of all IEEE-754 64-bit floating-point numbers.

Both are of different types, and automatic conversions will not occur.

The developer needs to write a code to handle this. Golang provides a standard inbuilt package strconv to convert string type to other types.

It is a common task of the developer to convert String to float or float to String.

How to Convert String to float in Golang?

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:

  • The string is an input string. if a 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 Stri`ng type.

  • One, is using fmt.Sprintf() function
  • Another way, 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 which contains following characters for different formats.

  • %f - Decimal format
  • %e - scientific notation format
  • %.5f- the precision with 5 format
  • %g - exponent format

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

Here is an 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:

string  
45.100000  
string  
4.510000e+01  
string  
45.10000  
string  
45.1  

strconv FormatFloat function example

strconv package has FormatFloat function 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:

  • f is a 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 - a large exponents
  • prec is of int precision value
  • bitSize is of int type that contains 32 and 64 values.

Here is a 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:

string  
5.78946  
string  
5.78946e+00  
string  
5.7895 
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.