# Float to int

**URL:** <https://discourse.julialang.org/t/float-to-int/44417>\
**Category:** New to Julia\
**Created:** [August 6, 2020, 12:43pm UTC](https://discourse.julialang.org/t/float-to-int/44417 "2020-08-06T12:43:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 6, 2020, 12:43pm UTC](https://discourse.julialang.org/t/float-to-int/44417/1 "2020-08-06T12:43:38Z")

</div>

hi all  
if i have array `c = [1.0, 2.0, 2.0, 1.0]` and i want to convert it to Int in this way `c = [1,2,2,1]`  
i tried some thing like that

```julia
for i in count
     i = Int64(i) 
     println(i) # the output her is 1 then 2 => like what i need 
end

println(count) # the output her is in float64 it didn't change

```

how i can solve that ?

and one more thing please

and if i need to Define an array  
how i can do that ?  
Suppose i need array with size 26

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 6, 2020, 12:54pm UTC](https://discourse.julialang.org/t/float-to-int/44417/2 "2020-08-06T12:54:44Z")

</div>

You cannot convert an array inplace, but you could just do `c = Int.(c)` to assign a new array of `Int`s to `c`.

---

<div class="post-metadata">

**Author:** ![Derek\_Vetsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derek_vetsch/32/8552_2.png) [@Derek\_Vetsch](https://discourse.julialang.org/u/Derek_Vetsch)\
**Post date:** [August 6, 2020, 1:04pm UTC](https://discourse.julialang.org/t/float-to-int/44417/3 "2020-08-06T13:04:43Z")

</div>

You can solve this with `broadcast`

```julia
c = [1.0, 2.0, 2.0, 1.0]
println(typeof(c))

c = Int.(c) #Change from Float to Int
println(typeof(c))

```

The dot between Int and c tells Julia to apply the Int conversion elementwise to the array.

Can you be more specific with what you are looking for for an array? If you are just looking to allocate space for a 26 element vector, you can do that by:  
`Array{Any, 1}(undef, 26)`

The Any tells Julia the type of the Array, the 1 tells Julia the dimension of the matrix(above gives you a vector), and undef would allocate the space, but not populate it with anything.

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 6, 2020, 1:22pm UTC](https://discourse.julialang.org/t/float-to-int/44417/4 "2020-08-06T13:22:35Z")

</div>

it was wonderful if it work

i tried this and their is an error (from REPL)

```julia
error during bootstrap:
LoadError("main.jl", 1, MethodError(Array{Any,1}, (Int64, 4), 0x0000000000006890))

```

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 6, 2020, 1:32pm UTC](https://discourse.julialang.org/t/float-to-int/44417/5 "2020-08-06T13:32:49Z")

</div>

this work

```julia
household = Array{Int64}(undef,length(a),1)

```

thanks

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 6, 2020, 1:48pm UTC](https://discourse.julialang.org/t/float-to-int/44417/6 "2020-08-06T13:48:59Z")

</div>

You don’t want the `1` there probably. That would create a `n x 1` matrix, and you probably want a vector instead.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 6, 2020, 1:53pm UTC](https://discourse.julialang.org/t/float-to-int/44417/7 "2020-08-06T13:53:29Z")

</div>

or

```julia

julia> c = [1.0, 2.0, 2.0, 1.0]
4-element Array{Float64,1}:
 1.0
 2.0
 2.0
 1.0

julia> cints = map(Int, c)
4-element Array{Int64,1}:
 1
 2
 2
 1

```

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [November 26, 2025, 1:47pm UTC](https://discourse.julialang.org/t/float-to-int/44417/8 "2025-11-26T13:47:21Z")

</div>

If you are not sure, whether conversion will be successful, e.g. after a json parsing, you can define

```julia
int_if_possible(v) = all(v .% 1 .== 0) ? Int.(v) : v

```

so

```julia-repl
julia> v = [1.0, 2.0, 3.0];

julia> int_if_possible(v)
3-element Vector{Int64}:
 1
 2
 3

julia> v = [1.0, 2.0, 3.1];

julia> int_if_possible(v)
3-element Vector{Float64}:
 1.0
 2.0
 3.1

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [November 26, 2025, 7:50pm UTC](https://discourse.julialang.org/t/float-to-int/44417/9 "2025-11-26T19:50:35Z")

</div>

> [@hhaensel](#):
>
> `v .% 1 .== 0`

You are looking for `isinteger`. But generally this is likely not a good approach. If you want Ints just use `round(Int, x)` or `trunc(Int, x)` or any related function. A mixture of exact Ints and floats is likely not very useful or meaningful.

Also please don’t resurrect ancient topics (half a decade in this case). If you have questions, just open a new topic (and link back if you think the context is still relevant).

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [November 26, 2025, 9:52pm UTC](https://discourse.julialang.org/t/float-to-int/44417/10 "2025-11-26T21:52:57Z")

</div>

Will do, thanks

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 26, 2025, 10:09pm UTC](https://discourse.julialang.org/t/float-to-int/44417/11 "2025-11-26T22:09:43Z")

</div>

> [@hhaensel](#):
>
> `all(v .% 1 .== 0)`

Also, this is potentially slow and needlessly allocates a temporary array. I suggest

```julia-auto
all(isinteger, v) 

```

instead.

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [November 26, 2025, 10:58pm UTC](https://discourse.julialang.org/t/float-to-int/44417/12 "2025-11-26T22:58:09Z")

</div>

Well, I understood that `isinteger` is by far the better option than my `x % 1 == 0`. But if we’re already continuing the discussion here, I’d like to add another version:

```julia
function int_if_possible(v::AbstractArray{T, N}) where {T, N}
    all(isinteger.(v)) ? Array{Int, N}(v) : v
end

```

which can handle multiple dimensions as well as empty arrays.

Moreover, here’s a benchmark on various versions. Interestingly, the broadcasting one is the fastest.

```julia
function int_if_possible_1(v)
    all(v .% 1 .== 0) ? Int.(v) : v
end

function int_if_possible_2(v::AbstractArray{T, N}) where {T, N}
    all(isinteger.(v)) ? Array{Int, N}(v) : v
end

function int_if_possible_3(v::AbstractArray{T, N}) where {T, N}
    all(isinteger, v) ? Array{Int, N}(v) : v
end

function int_if_possible_4(v::AbstractArray{T, N}) where {T, N}
    all_integer = true
    for x in v
        isinteger(x) || (all_integer=false; break)
    end
    all_integer ? Array{Int, N}(v) : v
end

v = 1:1.0:100_000

using BenchmarkTools

@btime int_if_possible_1(v);
# 981.000 μs (5 allocations: 793.61 KiB)

@btime int_if_possible_2(v);
# 350.400 μs (5 allocations: 793.61 KiB)

@btime int_if_possible_3(v);

# 477.300 μs (3 allocations: 781.32 KiB)

@btime int_if_possible_4(v);
# 475.200 μs (3 allocations: 781.32 KiB)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 27, 2025, 12:45pm UTC](https://discourse.julialang.org/t/float-to-int/44417/13 "2025-11-27T12:45:58Z")

</div>

> [@hhaensel](#):
>
> Well, I understood that `isinteger` is by far the better option than my `x % 1 == 0`

That wasn’t my point. My main point was that it is generally not a good idea to create intermediate arrays needlessly. So I suggest `all(isinteger, v)` instead of `all(isinteger.(v))` to avoid allocations.

It _can_ happen in some cases that allocating is faster, but generally, the non-allocating version is better.

Also, for this benchmark you should use variable interpolation for more accurate results, ie.

```julia-auto
@btime int_if_possible_3($v); 

```

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [November 27, 2025, 10:51pm UTC](https://discourse.julialang.org/t/float-to-int/44417/14 "2025-11-27T22:51:33Z")

</div>

Don’t worry, I did understood that and I was surprised to see that the allocating version was faster. That’s why I mentioned it. Maybe I should have been clearer.  
My main point, however, was the handling of multiple dimensions and empty arrays.
