In this tutorial, we will walk through some ways to convert a float64 to int in Golang. As introduced before, we can convert float64 to a string using the strconv package, then parse this string to an int. It is possible to convert from a float64 to an int this way but it seems clumsy. There are some more convenient ways for conversions. Let's explore them in today's post.
Example 1: Directly cast a float64 to an int
The most convenient way to convert from a float64 to an int is directly cast a float to an int. The below example shows how we can cast a float to an int in Golang:
package main
import "fmt"
func main() {
var float1 float64 = 25.36
var int1 int = int(float1)
fmt.Println(int1)
var float2 float64 = 27.00
var int2 int = int(float2)
fmt.Println(int2)
}
Output:
25
27
There is something to be noted here according to the Go specs:
- When converting a floating-point number to an integer, the fraction is discarded (truncation towards zero).
- Such casts have a problem in Go that can be unexpected: In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent. So if you use a very large value effectively as an infinity, and you convert it into an int, the result is maybe not accurate
package main
import (
"fmt"
"math"
)
func main() {
maxFloat := math.MaxFloat64
var int1 int = int(maxFloat)
fmt.Println(maxFloat)
fmt.Println(int1)
}
Output:
1.7976931348623157e+308
-9223372036854775808
Example 2: Using math.Round() before casting a float64 to an int
As mentioned above, when converting a floating-point number to an integer, the fraction is discarded (truncation towards zero). But in some cases, we might need to round the nearest int from float64. In that case, we can use the math.Round
first and then cast the float to the int:
package main
import (
"fmt"
"math"
)
func main() {
var x float64 = 5.7
var y float64 = 5.5
var z float64 = 5.4
fmt.Println(int(math.Round(x)))
fmt.Println(int(math.Round(y)))
fmt.Println(int(math.Round(z)))
}
Output:
6
6
5
Example 3: Using math.Ceil() before casting a float64 to an int
In some seniors, we want to ceil the int value. We can use math.Ceil()
before casting the float64 to int:
package main
import (
"fmt"
"math"
)
func main() {
var x float64 = 5.72
var y float64 = 5.01
var z float64 = 5.47
fmt.Println(int(math.Ceil(x)))
fmt.Println(int(math.Ceil(y)))
fmt.Println(int(math.Ceil(z)))
}
Output:
6
6
6
Example 4: Using the strconv package
In this example, we will first convert a float64 to a string, then use the Atoi()
function to convert this string to an int:
func Atoi(s string) (int, error)
: Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
package main
import (
"fmt"
"strconv"
)
func main() {
floats := []float64{1.9999, 2.0001, 2.0}
for _, f := range floats {
floatStr := fmt.Sprintf("%.0f", f)
if i, err := strconv.Atoi(floatStr); err == nil {
fmt.Println("Float number:", f, "/ Int number:", i)
} else {
fmt.Println("Float number:", f, "/ Int number:", i)
}
}
}
Output:
Float number: 1.9999 / Int number: 2
Float number: 2.0001 / Int number: 2
Float number: 2 / Int number: 2
Noted that we always need to use floatStr := fmt.Sprintf("%.0f", f)
as conversion from float64 to string or the Atoiv()
function will fail and the int always is 0.
Summary
So in today's post, I have introduced to you some methods to convert from float64 to int. Care should be taken for the boundary cases (MaxFloat64 and MinFloat64) because if not carefully used, the conversion will fail and cause logic errors to the program.
References
https://pkg.go.dev/strconv
https://go.dev/ref/spec#Conversions