×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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:

  • float32
  • float64

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.

  1. The default value of a float variable is ___.
  2. Which float type provides higher precision?
  3. What is the result of adding 0.1 and 0.2 in Go?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.