Home »
Golang
Go Float Data Types
Last Updated : May 19, 2025
In Go, float data types are used to store numbers with decimal points and they are useful for precision and fractional handling.
Go provides two floating-point data types:
The following table show the number of precisions and memory sizes of the specific float types (float32
and float64
):
Type |
Precision |
Memory Size |
float32 |
Approximately 6-7 decimal digits |
4 bytes |
float64 |
Approximately 15 decimal digits |
8 bytes |
Declare Float Variables
You can declare float variables using the var
keyword or shorthand declaration. By default, floating-point literals are treated as float64
.
var price float32 = 9.99
pi := 3.14159
Example
In this example, we declare and print float variables:
package main
import "fmt"
func main() {
var price float32 = 19.99
var discount float64 = 5.25
finalPrice := price - float32(discount)
fmt.Println("Price:", price)
fmt.Println("Discount:", discount)
fmt.Println("Final Price:", finalPrice)
}
When executed, this program outputs:
Price: 19.99
Discount: 5.25
Final Price: 14.74
Default Value of Floats
When float variables are declared without initialization, they get a default value of 0
.
Example
The following example shows the default value of float type:
package main
import "fmt"
func main() {
var average float64
fmt.Println("Default value of average:", average)
}
When executed, this program outputs:
Default value of average: 0
Float Arithmetic
Go supports arithmetic operations for float types: +
, -
, *
, and /
. Note that the modulo operator %
is not allowed with float types.
Example
The following example shows the arithmetic operations on float types:
package main
import "fmt"
func main() {
a := 10.5
b := 3.0
fmt.Println("Addition:", a + b)
fmt.Println("Subtraction:", a - b)
fmt.Println("Multiplication:", a * b)
fmt.Println("Division:", a / b)
}
When executed, this program outputs:
Addition: 13.5
Subtraction: 7.5
Multiplication: 31.5
Division: 3.5
Precision Issues
Due to how floating-point numbers are represented in memory, precision issues can occur when performing calculations.
Example
The following example shows the precision issue while printing the float value:
package main
import "fmt"
func main() {
a := 0.1
b := 0.2
sum := a + b
fmt.Println("Sum:", sum)
}
When executed, this program outputs:
Sum: 0.30000000000000004
Conversion Between Float Types
You can convert between float32
and float64
using explicit type conversion. You need to specify the specific float type in which you want to convert a value before the variable / value.
Example
The following example shows how you can make the conversion between the float types:
package main
import "fmt"
func main() {
var small float32 = 12.34
var large float64
large = float64(small)
fmt.Println("Converted value:", large)
}
When executed, this program outputs:
Converted value: 12.34
Float Data Types Exercise
Select the correct option to complete each statement about float data types in Go.
- The default value of a float variable is ___.
- Which float type provides higher precision?
- What is the result of adding 0.1 and 0.2 in Go?
Advertisement
Advertisement