How to Convert Golang Float to Int

An integer is an immutable data type, so converting a float to an int is the most common operation. Before starting our main course, let’s see the float and integer data types.

What is float in Golang?

The float data type in Golang is used to store positive and negative numbers with a decimal point. 

The float data type has two types of floating-point values:

Type Size Range
float32 32-bits -3.4e+38 to 3.4e+38.
float64 64-bits -1.7e+308 to +1.7e+308.

The default data type for float in Golang is float64. If you don’t specify the class, it will be float64.

How to create a float variable in Go?

To create a float variable in Golang, use the var float_var_name float64 syntax. To create a float32 variable, use the var float_var_name float32 syntax.

What is int in Golang?

An integer is a numeric data type without a decimal point in Go. The integer data type has several versions: int8, int16, int32, int64, uint8, uint16, uint32, and uint64.

Golang float to int

To convert a float to int in Golang, use the int() function. The int() is a built-in function that accepts an argument and returns the integer representation of the input value.

When converting a floating point to an integer value, pass a float value to the int() function, and it returns an integer value.

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var floating_point float64 = 21.19
  fmt.Println(floating_point)
  fmt.Print("The data type is: ", reflect.TypeOf(floating_point), "\n")
  
  var integer int = int(floating_point)
  fmt.Println(integer)
  fmt.Print("The data type is: ", reflect.TypeOf(integer), "\n")
}

Output

21.19
The data type is: float64
21
The data type is: int

In this example, we are declaring float64 value and printing the data type using reflect.TypeOf() function.

In the next step, an int() function was used to convert a float64 value to int and print its value and data type.

When converting a floating-point number to an integer, the fraction is discarded.

Converting float to int with rounding numbers

To round numbers while converting float to int, use math.Round() function. Apply the int() function to the result of math.Round() function, and it will convert to an integer.

package main

import (
  "fmt"
  "math"
  "reflect"
)

func main() {
  var floating_point float64 = 21.67
  fmt.Println(floating_point)
  fmt.Print("The data type is: ", reflect.TypeOf(floating_point), "\n")
 
  var integer int = int(math.Round(floating_point))
  fmt.Println(integer)
  fmt.Print("The data type is: ", reflect.TypeOf(integer), "\n")
}

Output

21.67
The data type is: float64
22
The data type is: int

Converting float to int with rounding up

To round up numbers while converting float to int, use math.Ceil() function. Apply the int() function to the result of math.Ceil() function and it will convert to an integer.

package main

import (
  "fmt"
  "math"
  "reflect"
)

func main() {
  var floating_point float64 = 21.67
  fmt.Println(floating_point)
  fmt.Print("The data type is: ", reflect.TypeOf(floating_point), "\n")
 
  var integer int = int(math.Ceil(floating_point))
  fmt.Println(integer)
  fmt.Print("The data type is: ", reflect.TypeOf(integer), "\n")
}

Output

21.67
The data type is: float64
22
The data type is: int

Converting float to int with rounding down

To round down numbers while converting float to int, use math.Floor() function. Apply the int() function to the result of math.Floor() function and it will convert to an integer.

package main

import (
  "fmt"
  "math"
  "reflect"
)

func main() {
  var floating_point float64 = 21.67
  fmt.Println(floating_point)
  fmt.Print("The data type is: ", reflect.TypeOf(floating_point), "\n")
  
  var integer int = int(math.Floor(floating_point))
  fmt.Println(integer)
  fmt.Print("The data type is: ", reflect.TypeOf(integer), "\n")
}

Output

21.67
The data type is: float64
21
The data type is: int

Final words

The best way to convert a float to int in Golang, use the int() function.

To round up while converting a float to int, use the math.Ceil() function.

To round down while converting a float to int, use the math.Floor() function.

That’s it.

Leave a Comment