Golang Program to convert string variables to double


In this tutorial, we will learn how to convert string variables to double in Go programming language.

String is a data type which is used to store characters or alphabets. Strings have a size of 1 byte or 8 bits.

Whereas double is a data type which is used to store double precision floating point numbers. The size of double is 64 bits or 8 bytes.

Program to Convert String type Variable to Double Using ParseFloat() Method

Syntax

func ParseFloat (s string, bitsize int) (float 64, error)

ParseFloat() function is used to convert strings to float type values. This function accepts two arguments one is the string that we wish to convert and other is a integer type value. This value specifies the size of the result. It can be either 32 or 64. This function returns the final result as a 64 bit float type number and an error which can be printed on the screen if any problem occurs in the conversion process. If we wish to get the result in 32 bit float number then we have to specify the bitsize as 32 this makes the result convertible to 32 bit float number.

Algorithm

  • Step 1 − Import the package fmt, reflect and strconv

  • Step 2 − Start function main().

  • Step 3 − Declare the String variable named ‘string’ and assign a value to it.

  • Step 4 − Display the data type of variable using reflect.typeof() function.

  • Step 5 − Convert the string to Double using ParseFloat() Method.

  • Step 5 − Store the converted type in a variable and print the result on the screen using the fmt.Println() function.

Example

package main // import the required packages to perform the task import ( "fmt" "reflect" "strconv" ) // this is the main function func main() { // initialize the srting variable string := "3.1415926535" // print the type of variable. fmt.Println("Type of variable Before conversion is :", reflect.TypeOf(string)) // print the value of string fmt.Println("String value is: ", string) // use the ParseFloat() Function on string to convert it into float x, _ := strconv.ParseFloat(string, 64) // print the type of variable fmt.Println("Type of variable After conversion is :", reflect.TypeOf(x)) // print the value of variable fmt.Println("Float value is: ", x, "\n") // initialize the srting string1 := "222.222" // print the type of variable fmt.Println("Type of variable Before conversion is :", reflect.TypeOf(string1)) // print the value fmt.Println("String value is: ", string1) // use the Parsefloat() Function on string y, _ := strconv.ParseFloat(string1, 64) //used the bitsize=32, value may vary // print the type of variable fmt.Println("Type of variable After conversion is :", reflect.TypeOf(y)) // print the value of variable fmt.Println("Float value is: ", y, "\n") }

Output

Type of variable Before conversion is : string 
String value is: 3.1415926535 
Type of variable After conversion is : float64 
Float value is: 3.1415926535 

Type of variable Before conversion is : string 
String value is: 222.222 
Type of variable After conversion is : float64 
Float value is: 222.222

Description of the Code

  • First, we Iimport the package fmt, reflect and strconv. fmt package is used to print anything on the screen. Reflect package is used to get the present data type of the variable. Strconv package is used to get other pre-defined functions like ParseFloat().

  • Then we start the main() function to perform the task and convert the data type of string to Double.

  • Initialize and define a variable of type string and assign value to it.

  • In the next step, we are printing the type of the variable we have just declared using TypeOf() function defined in strconv package.

  • Then we print the actual value that the data type has in it using fmt.Print() function.

  • Then we are calling ParseFloat() function from the “strconv” package of go language, and pass the string to the function to convert the string value to doubel.

  • Now we have printed the type of variable, to check if the data type is changed from string to Double or not.

  • Now store the results in a separate variable and print them on the screen using fmt.Print() function.

Conclusion

We have successfully compiled and executed the Golang program code to convert string type variables into Double using function.

Updated on: 14-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements