- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to convert double type variables to string
In this tutorial we will learn how to convert double(float64) type variables to string variables using Golang programming language.
Double stands for double precision. In programming languages, a double data-type is used to handle decimal numbers more precisely i.e. using double data type we can store more number of digits after a decimal point with accuracy.
String data type is used to store a sequence of characters. It can be in the form of literals or alphabets. The size of string variable is 1 byte or 8 bits.
EXAMPLE 1: GO LANGUAGE CODE TO CONVERT DOUBLE TYPE VARIABLE TO STRING USING Itoa() function.
Syntax
func FormatFloat(x float, fmt byte, prec, bitsize int) string
FormatFloat − this function is used to convert decimal numbers to strings. It takes four arguments the first one is the float number that we wish to convert followed by fmt and prec which are used to control the output and the last argument is the bitsize of integer result. It can be 32 or 64 bits.
Example
package main import ( "fmt" "strconv" ) // fmt package provides the function to print anything // Package strconv allows us to use other predefined methods like FormatFloat() func main() { // initialize a number variable and asign float type value to it var number float64 = 3.14159 // printing the decimal type value fmt.Printf("Type of variable before conversion : %T \nValue : %v\n", number, number) // converting double to string output := strconv.FormatFloat(number, 'g', 5, 64) // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", output, output) // print a new line fmt.Println() // reasign float type value to it number = -285.32 // printing the decimal type value fmt.Printf("Type of variable before conversion : %T \nValue : %v\n", number, number) // converting double to string output = strconv.FormatFloat(number, 'g', 5, 64) // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", output, output) }
Output
Type of variable before conversion : float64 Value : 3.14159 Type of variable after conversion : string Value : 3.1416 Type of variable before conversion : float64 Value : -285.32 Type of variable after conversion : string Value : -285.32
Description of the Code
In the above program, we first declare the package main.
We imported the fmt package that includes the files of package fmt that allows us to print anything on the screen
Next, we start the function main().
Now declare and initialize a double type variable named number and assign value to it.
Print the type and value of the double type variable.
Use FormatFloat() function to convert double type value to string.
Next, we print the converted int value along with the data type, using the Printf() function.
Re assign different values of double variable and convert it to string in upcoming examples and print the result.
EXAMPLE 2: GO LANGUAGE CODE TO CONVERT DOUBLE TYPE VARIABLE TO STRING USING Sprintf() FUNCTION.
Syntax
func Sprintf(format string, a ...interface{}) string
Sprintf() function returns a formatted string. The first argument should be a string format followed by a variable number of arguments. This function then returns the result as a formatted string format.
Example
// Golang program to convert Int data type to Float using Sprintf() function package main // fmt package provides the function to print anything import ( "fmt" ) // start the main function this is the main starting point of the program func main() { // declare the variable and assign a value to it var number float64 = 273.54 // printing the float type value fmt.Printf("Type of variable before conversion : %T \nValue : %v\n", number, number) // use the Sprintf() method on the the variable to convert it to string and store the // result in a seperate variable. output := fmt.Sprintf("%v", number) // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", output, output) // printing a new line fmt.Println() // reassign a value to it number = -73.54 // printing the float type value fmt.Printf("Type of variable before conversion : %T \nValue : %v\n", number, number) // use the Sprintf() method on the the variable to convert it to string and store the // result in a seperate variable. output = fmt.Sprintf("%v", number) // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", output, output) }
Output
Type of variable before conversion : float64 Value : 273.54 Type of variable after conversion : string Value : 273.54 Type of variable before conversion : float64 Value : -73.54 Type of variable after conversion : string Value : -73.54
Description of the Code
In the above program, we first declare the package main
We imported the fmt package that includes the files of package fmt and allows us to print anything on the screen.
Next, we start the function main() and this function is the entry point of the executable program. It does not take any argument nor return anything.
Now declare and initialize a double precision value to a variable, which we will later convert to string
Printing the type and value of that double variable.
Use Sprintf() function to convert double type data to string type value.
Store the output obtained in a separate variable called result.
Next, we print the converted int value along with the data type, using the Printf() function.
You can re-assign different values of double and convert it to string in upcoming examples.
Conclusion
We have successfully compiled and executed a go language program to convert double type variables to string along with examples.
- Related Articles
- Golang Program to convert string variables to double
- Golang Program to convert double type variables to int
- Golang Program to convert int type variables to String
- Golang Program to convert string type variables into Boolean
- Golang Program to convert string type variables into int
- Swift program to convert double type variables to int
- C++ Program to convert double type Variables into int
- Golang Program to convert long type variables to int
- Golang Program to convert int type variables to long
- Golang Program to convert boolean variables into string
- Swift program to convert double type variable to string
- Golang Program to Convert Char Type Variables to Int Using Inbuilt Functions
- C++ Program to Convert String Type Variables into Boolean
- C++ Program to Convert int Type Variables into String
- C++ Program to convert int Variables into double
