Multiple ways to Convert(Cast) String to/from Float in Golang
- Admin
- Dec 31, 2023
- Golang-examples
This post covers converting (cast) the 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 code to handle this. Golang provides a standard inbuilt package strconv to convert string types 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 the 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
Golang Convert float number to String Example
There are two ways to convert the float type to a String type.
- One, is using
fmt.Sprintf()function - Another way, is using the
strconv.FormatFloatfunction
Convert float number to String using golang 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 that contains the 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
Convert float to String using golang strconv FormatFloat function example
strconv package has a FormatFloat function to convert the floating number to string with a given format and precision. Here is the syntax of this function
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
Arguments:
fis a floating number converted to a stringfmtis of byte contains values b -binary exponent, e or E - Decimal exponent, F - no exponent, g or G - a large exponentsprecis of int precision valuebitSizeis 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
