To convert a string
type value to a float
type value, you can use the ParseFloat()
method from the strconv
standard package in Go or Golang.
The ParseFloat()
method:
- accepts a
string
type value as its first argument - a bit size of
int
type value as its second argument - and returns 2 values, the first one being the converted
float
type value if any and the second being theerror
data if any.
TL;DR
package main
// import `strconv` package
import (
"fmt"
"strconv"
)
func main() {
// convert `string` type value of "33.96"
// using the `ParseFloat()` method and passing
// the value "33.96" as its first argument
// 64, the bit size as its second argument
convertedFloatNum, err := strconv.ParseFloat("33.96", 64)
// check if any error happened
if err != nil {
fmt.Println(err.Error())
return
}
// log output of the `convertedFloatNum`
// variable to the console
fmt.Println(convertedFloatNum) // 33.96 and the type is float ✅
}
For example, let's say we need to convert a string
type value of "33.96"
to its corresponding float
type value.
To do that let's first import the strconv
standard package like this,
package main
// import `strconv` package
import "strconv"
After importing the package, inside the main()
function we can use the ParseFloat()
method from the strconv
package and pass the string
type value of "33.96"
as its first argument, and 64
the bit size as its third argument to the function.
It can be done like this,
package main
// import `strconv` package
import "strconv"
func main(){
// convert `string` type value of "33.96"
// using the `ParseFloat()` method and passing
// the value "33.96" as its first argument
// 64, the bit size as its second argument
strconv.ParseFloat("33.96", 64)
}
The ParseFloat()
method returns 2 values where the first value is the converted float
type value if successfully converted and the second value is the error
data if anything happened during the conversion. To capture these 2 data, let's assign 2 variables.
It can be done like this,
package main
// import `strconv` package
import "strconv"
func main(){
// convert `string` type value of "33.96"
// using the `ParseFloat()` method and passing
// the value "33.96" as its first argument
// 64, the bit size as its second argument
convertedFloatNum, err := strconv.ParseFloat("33.96", 64)
}
Before dealing with the converted float
value we need to check if there was any error during the conversion process. Let's use a simple if
conditional statement and check to see if the err
data variable is nil
or not. If it is not nil
, then there must have been an error that happened during the conversion.
It can be done like this,
package main
// import `strconv` package
import (
"fmt"
"strconv"
)
func main(){
// convert `string` type value of "33.96"
// using the `ParseFloat()` method and passing
// the value "33.96" as its first argument
// 64, the bit size as its second argument
convertedFloatNum, err := strconv.ParseFloat("33.96", 64)
// check if any error happened
if err != nil {
fmt.Println(err.Error())
return
}
}
If there is no error then it is safe to assume that the convertedFloatNum
variable must be having the converted float
type value. Let's log the output to the console like this,
package main
// import `strconv` package
import (
"fmt"
"strconv"
)
func main() {
// convert `string` type value of "33.96"
// using the `ParseFloat()` method and passing
// the value "33.96" as its first argument
// 64, the bit size as its second argument
convertedFloatNum, err := strconv.ParseFloat("33.96", 64)
// check if any error happened
if err != nil {
fmt.Println(err.Error())
return
}
// log output of the `convertedFloatNum`
// variable to the console
fmt.Println(convertedFloatNum) // 33.96 and the type is float ✅
}
As you can see that the string
type value of "33.96"
is successfully converted to its corresponding float
type value which is what we want.
See the above code live in The Go Playground.
Now let's try to give a string
type value composed of alphabets like John Doe
and then try to convert it to its float
type value like this,
package main
// import `strconv` package
import (
"fmt"
"strconv"
)
func main() {
// convert `string` type value of "John Doe"
// using the `ParseFloat()` method and passing
// the value "John Doe" as its first argument
// 64, the bit size as its third argument
convertedFloatNum, err := strconv.ParseFloat("John Doe", 64) // Error ❌. strconv.ParseFloat: parsing "John Doe": invalid syntax
// check if any error happened
if err != nil {
fmt.Println(err.Error())
return
}
// log output of the `convertedFloatNum`
// variable to the console
fmt.Println(convertedFloatNum)
}
As you can see that the Go compiler shows us an error saying that strconv.ParseFloat: parsing "John Doe": invalid syntax
which essentially means that it cannot parse this value to its corresponding float
type value.
See the above code live in The Go Playground.
That's all 😃!