How to Convert Float to Int in Golang

To convert a float to int in Golang, you can “use the int() function.” The int() function accepts an argument and returns the integer representation of the input 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.

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.

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.

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.

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.

Converting float64 to int

To convert a float64 to int in Go, you can use the “int()” function. For example, if var a float64 = 2.1, the int(a) function returns as an integer output value. 

package main

import (
 "fmt"
)

func main() {
  var a float64 = 2.1
  var b int = int(a)
  fmt.Println(b)
}

Output

2

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

That’s it.

Related posts

Go string to int

1 thought on “How to Convert Float to Int in Golang”

Comments are closed.