However, type conversion becomes more than essential when you are working with a statically-typed programming language.
One common type conversion task is converting a floating-point value, also known as floats, into string representation.
In this tutorial, we will learn all about float conversion in Go. We will explore all the methods and tools that we can use to convert the floats to string representation.
The Basics
In Go, a floating-point value refers to a numeric data type that allows us to represent the real numbers with decimal points.
There are two main types of floating-point values:
- float32 – The float32 type allows us to represent a single-precision floating point value. It uses 32-bits to store the value and has a range of -3.4e38 to 3.4e38. It also has a precision of up to seven decimal places.
- float64 – The float64 type on the hand allows us to represent the double-precision floating-point values. It uses 64 bits to store the value and hence has a significantly larger size and precision compared to the 32-bit equivalent. It has a range of -1.8e308 to 1.8e308. This one has a precision of up to 15 decimal places.
Define a Float in Golang
In Go, we can define a float like we would with any data type. The following example shows how to use the static typing to define the floats in Golang:
var float32Value float32 = 3.14159
var float64Value float64 = 3.141592653589793
Once we define the float, we can proceed and use it to perform a wide variety of operations including type conversion, mathematical operations, and more.
Convert a Float to String in Golang
Let us now explore the various methods that we can use to convert a float into a string as demonstrated in the next sections.
Method 1: Using Fmt.Sprintf
The first and most common method is the Sprintf() function from the “fmt” package. We can use this function to quickly convert a float into a string s demonstrated in the following example:
import (
"fmt"
)
func main() {
f := 3.14159
stringValue := fmt.Sprintf("%f", f)
fmt.Println(stringValue)
}
In the given example, we use the “fmt.Sprintf” method with the format specifier “%f” to convert the float into a string.
We can also control the precision and the formatting of the resulting string by making the modifications to the format specifier.
An example is as follows:
import (
"fmt"
)
func main() {
f := 3.141592653589793
s1 := fmt.Sprintf("%.2f", f) // Two decimal places
s2 := fmt.Sprintf("%.4f", f) // Four decimal places
s3 := fmt.Sprintf("%.0f", f) // round to nearest int.
}
In this example, we modify the format specifiers of .2f, .4f, .Of, 3 and the precision of the value. We can control how the float is converted into the string including the number or decimal places.
Method 2: Using Strconv.FormatFloat
When it comes to type conversion in Go, the “strconv” package plays a key part in that. This package provides conversion to and from the string representation from other Golang data types.
From this package, we can use the FormatFloat() function to convert a float to a string.
The following example shows how to use this function to convert a float to string:
import (
"fmt"
"strconv"
)
func main() {
f := 3.14159
s := strconv.FormatFloat(f, 'f', -1, 64)
fmt.Println(s)
}
In this example, we use the “FormatFloat” function from the “strconv” package to convert the float into a string.
This function provides more functionality and control of the conversion process as demonstrated in the second argument which allows us to specify the format. In this case, setting the value to “f” means decimal.
The third argument allows us to specify the precision for the conversion type. In this case, the default precision is -1.
Lastly, the fourth argument in the function allows us to specify the bit size. In this case, we specify the value as 65 for 64-bit floating (float64).
We can also specify a custom precision when converting a float to a string.
floatValue := 3.14159
stringValue := strconv.FormatFloat(floatValue, 'f', 2, 64)
fmt.Println(stringValue)
}
In this case, we specify the precision of the resulting string to two decimal places.
Method 3: Exponential Notation (Sprintf)
Although this is not technically a new method of converting a float to string, it can allow to use a custom format for the resulting string.
This involves specifying the format specifier to “%e” or “%E” to represent a float into its exponential notation.
An example is as follows:
import (
"fmt"
)
func main() {
f := 3.141592653589793
s := fmt.Sprintf("%e", f)
fmt.Println(s)
}
This converts the float to a string in exponential notation.
Conclusion
In this tutorial, we learned about one of the most fundamental and common type conversions in Go. We covered the methods such as Sprintf and FormatFloat to convert a float into a string.