Converting a string variable into Boolean, Integer or Float type in Golang
Last Updated :
10 May, 2020
Various types of string conversions are needed while performing tasks in Golang. Package
strconv is imported to perform conversions to and from string.
String to Boolean Conversion
ParseBool is used to convert string to boolean value. It accepts 1, t, T, TRUE, true, True as
true and 0, f, F, FALSE, false, False as
false. Any other value returns an error and will display the value as false.
Example:
C
// Golang program to convert a string
// into Boolean data type
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
str := "GeeksforGeeks"
fmt.Println("Before :", reflect.TypeOf(str))
fmt.Println("String value is: ", str)
b, _ := strconv.ParseBool(str)
fmt.Println("After :", reflect.TypeOf(b))
fmt.Println("Boolean value is: ", b, "\n")
str1 := "t"
fmt.Println("Before :", reflect.TypeOf(str1))
fmt.Println("String value is: ", str1)
b1, _ := strconv.ParseBool(str1)
fmt.Println("After :", reflect.TypeOf(b1))
fmt.Println("Boolean value is: ", b1, "\n")
str2 := "0"
fmt.Println("Before :", reflect.TypeOf(str2))
fmt.Println("String value is: ", str2)
b2, _ := strconv.ParseBool(str2)
fmt.Println("After :", reflect.TypeOf(b2))
fmt.Println("Boolean value is: ", b2, "\n")
}
Output:
Before : string
String value is: GeeksforGeeks
After : bool
Boolean value is: false
Before : string
String value is: t
After : bool
Boolean value is: true
Before : string
String value is: 0
After : bool
Boolean value is: false
String to Integer Conversion
ParseInt function is used to convert string to an integer value. It interprets a string in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value.
Example:
C
// Golang program to convert String
// into Integer Data type
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
str := "GeeksforGeeks"
fmt.Println("Before :", reflect.TypeOf(str))
fmt.Println("String value is: ", str)
i, _ := strconv.ParseInt(str, 10, 64)
fmt.Println("After :", reflect.TypeOf(i))
fmt.Println("Integer value is: ", i, "\n")
str1 := "100"
fmt.Println("Before :", reflect.TypeOf(str1))
fmt.Println("String value is: ", str1)
i1, _ := strconv.ParseInt(str1, 10, 64)
fmt.Println("After :", reflect.TypeOf(i1))
fmt.Println("Integer value is: ", i1, "\n")
}
Output:
Before : string
String value is: GeeksforGeeks
After : int64
Integer value is: 0
Before : string
String value is: 100
After : int64
Integer value is: 100
String to Float Conversion
ParseFloat is used to convert the string to float type with the precision specified by bitSize: 32 for float32, or 64 for float64. When bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value.
Example:
C
// Golang program to convert
// String into Float Data type
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
str := "3.1415926535"
fmt.Println("Before :", reflect.TypeOf(str))
fmt.Println("String value is: ", str)
f, _ := strconv.ParseFloat(str, 64)
fmt.Println("After :", reflect.TypeOf(f))
fmt.Println("Float value is: ", f, "\n")
str1 := "3.1415926535"
fmt.Println("Before :", reflect.TypeOf(str1))
fmt.Println("String value is: ", str1)
f1, _ := strconv.ParseFloat(str1, 32)
fmt.Println("After :", reflect.TypeOf(f1))
fmt.Println("Float value is: ", f1, "\n")
}
Output:
Before : string
String value is: 3.1415926535
After : float64
Float value is: 3.1415926535
Before : string
String value is: 3.1415926535
After : float64
Float value is: 3.1415927410125732
Similar Reads
Different Ways to Convert an Integer Variable to String in Golang Integer variable cannot be directly convert into String variable. In order to convert string to integer type in Golang , you have store value of integer variable as string in string variable. For this, we are using strconv and fmt package functions. 1. Itoa() Function:Â The Itoa stands for Integer t
3 min read
Different Ways to Convert the Boolean Type in String in Golang In order to convert Boolean Type to String type in Golang , you can use the strconv and fmt package function. 1. strconv.FormatBool() Method: The FormatBool is used to Boolean Type to String. It returns "true" or "false" according to the value of b. Syntax: func FormatBool(b bool) string Example : C
2 min read
How to fetch an Integer variable as String in GoLang? To fetch an Integer variable as String, Go provides strconv package which has methods that return a string representation of int variable. There is nothing like an integer variable is converted to a string variable, rather, you have to store integer value as a string in a string variable. Following
2 min read
How to Convert string to float type in Golang? ParseFloat function is an in-build function in the strconv library which converts the string type into a floating-point number with the precision specified by bit size. Example: In this example the same string -2.514 is being converted into float data type and then their sum is being printed. Once i
1 min read
How to Convert string to integer type in Golang? Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can
2 min read
How to convert Int data type to Float in Golang? In Golang, the data types are bound to variables rather than the values, which means that, if you declare a variable as int, then you can store only integer type value in it, you cant assign character or string in it unless you convert the data type to required data type. To convert an integer data
2 min read