Convert float64 to int or int64 in Go (cast, round, strconv)

Golang float64 to int and golang float64 to int64: convert with int()/int64() truncation, math.Round for nearest integer, golang convert float to int and golang convert float64 to int safely, plus float64 to int golang notes on overflow and strconv.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Convert float64 to int or int64 in Go (cast, round, strconv)

Common searches—golang float64 to int, golang float64 to int64, float64 to int golang, go float64 to int, golang convert float to int, golang convert float64 to int, float to int golang, and go float64 to int64—all map to the same core idea: turn a floating-point value into a whole number type. The usual answer is a conversion (int(x) or int64(x)), optionally after math.Round, math.Ceil, or math.Floor. This guide shows each pattern, calls out spec rules on conversions, and warns about overflow behavior.

Tested with Go 1.24 on Linux.


Golang float64 to int and int64: direct cast (truncation)

The most direct golang convert float64 to int approach is a conversion expression. The fraction is discarded (truncation toward zero), not “nearest” rounding.

go
package main

import "fmt"

func main() {
	var f float64 = 25.36
	fmt.Println(int(f), int64(f))

	var g float64 = -3.9
	fmt.Println(int(g), int64(g))
}
Output

Running this prints 25 25 then -3 -3 (not -4).

Overflow and very large floats

Per the Go spec, when the integer type cannot represent the floating-point value, the conversion still succeeds but the result is implementation-defined. Casting math.MaxFloat64 to int is therefore meaningless for portable logic:

go
package main

import (
	"fmt"
	"math"
)

func main() {
	x := math.MaxFloat64
	fmt.Println(int(x))
}
Output

On typical amd64 builds this prints a large negative sentinel (-9223372036854775808), not a “saturated” max int—so validate magnitude before casting.


Round, ceil, or floor before converting

When golang float to int must follow mathematical rounding instead of truncation, apply a math helper before the cast.

math.Round (nearest integer, half away from zero)

go
package main

import (
	"fmt"
	"math"
)

func main() {
	for _, x := range []float64{5.7, 5.5, 5.4, -2.5} {
		fmt.Println(x, "->", int(math.Round(x)))
	}
}
Output

With Go’s math.Round (ties half away from zero), 5.5 becomes 6 and -2.5 becomes -3.

math.Ceil and math.Floor

Use math.Ceil when you need the smallest integer ≥ the value, and math.Floor for the largest integer ≤ the value—common for counts, pagination, and bucket sizing.

go
package main

import (
	"fmt"
	"math"
)

func main() {
	x := 5.01
	fmt.Println("ceil:", int(math.Ceil(x)))
	fmt.Println("floor:", int(math.Floor(x)))
}
Output

For x := 5.01 this prints ceil: 6 and floor: 5.


strconv path (string round-trip)

Sometimes you already format floats as strings. You can combine fmt.Sprintf (or strconv.FormatFloat) with strconv.Atoi or strconv.ParseInt. This is more work and easier to get wrong than a cast; use it when the string is the source of truth.

go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	for _, f := range []float64{1.9999, 2.0001, 2.0} {
		s := fmt.Sprintf("%.0f", f)
		i, err := strconv.Atoi(s)
		if err != nil {
			fmt.Println("Float:", f, "error:", err)
			continue
		}
		fmt.Println("Float:", f, "int:", i)
	}
}

Example output:

text
Float: 1.9999 int: 2
Float: 2.0001 int: 2
Float: 2 int: 2

Here %.0f rounds the string representation; it is not the same as int(f) truncation. Choose the path that matches your rounding rules.


Summary

For golang float64 to int or golang float64 to int64, start with int(x) / int64(x) when truncation toward zero is correct. For golang convert float to int with rounding, call math.Round (or Ceil / Floor) first, then cast. Watch range: huge floats do not convert to sensible integers. strconv is optional when you truly work through decimal strings; otherwise prefer numeric conversions for clarity and speed. See the conversion rules and strconv for edge cases.


References


Frequently Asked Questions

1. How do I convert golang float64 to int?

Use a conversion expression int(x); the fractional part is discarded (truncation toward zero), same rule as for int64(x) when you need golang float64 to int64.

2. What is the difference between golang convert float to int and rounding first?

A bare cast truncates toward zero; use math.Round, math.Floor, or math.Ceil before the cast when you need nearest integer, ceiling, or floor semantics.

3. Does float64 to int golang conversion check overflow?

If the float value is too large or too small for the integer type, the conversion is still allowed but the bit pattern is implementation-defined; guard with range checks or avoid casting extreme values like math.MaxFloat64.

4. When should I use strconv instead of a cast?

Prefer a direct numeric cast for performance; use strconv when the value already lives in string form or you must match a specific decimal format before parsing to int.

5. Is golang float to int the same as float64 to int?

Untyped float constants and float32 values are converted to float64 when needed; the same truncation rules apply once you convert to int or int64.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …