Some Float64 numbers have Integer values (e.g. -4.0, 0.0, 1.0, 64.0). They convert to Int64 directly.
julia> Int64(-4.0), Int64(0.0), Int64(64.0)
(-4, 0, 64)
Some Float64 values are too large (positive or negative) to fit in an Int64. Many Float64 values exceed the magnitude of largest Int64 (typemax(Int64)
).
julia> Int64(Float64(typemax(Int64)/2))
4611686018427387904
julia> Int64(3*Float64(typemax(Int64)/2))
ERROR: InexactError: Int64(1.3835058055282164e19)
Stacktrace:
[1] Int64(x::Float64)
@ Base .\float.jl:994
[2] top-level scope
@ REPL[26]:1
To convert Float64 values that are small enough for Int64s, you must set the fractional part of the Float64 to 0
(e.g. 3.14 → 3.0 or 4.0, 5.5 → 5.0 or 6.0). There are different rounding modes you may choose to use for this purpose.
pos = 2.5
neg = -2.5
round(pos, RoundUp), round(neg, RoundUp)
# (3.0, -2.0)
round(pos, RoundDown), round(neg, RoundDown)
# (2.0, -3.0)
round(pos, RoundToZero), round(neg, RoundToZero)
# (2.0, -2.0)
round(pos, RoundFromZero), round(neg, RoundFromZero)
# (3.0, -3.0)
# RoundNearest resolves values halfway between (x.5)
# by choosing the nearest even
round(pos, RoundNearest), round(neg, RoundNearest)
# (2.0, -2.0)
# RoundNearestTiesAway and RoundNearestTiesUp also work round(pos, RoundNearestTiesAway), round(neg, RoundNearestTiesAway)
# (3.0, -3.0)
round(3.5, RoundNearestTiesAway), round(-3.5 RoundNearestTiesAway)
# (4.0, -4.0)
round(pos, RoundNearestTiesUp), round(neg, RoundNearestTiesUp)
# (3.0, -2.0)
Whichever rounding mode you use, once the fractional part is rounded away, it will convert to an Int64 as long as it fits.
Int64(round(pi, RoundUp)), Int64(round(-pi, RoundNearest))
(4, -3)
Julia lets you combine the two steps.
round(Int64, pi, RoundUp), round(Int64, -pi, RoundNearest)
(4, -3)
fyi trunc(Int64, x)
works like Int64(round(x, RoundToZero)).