# Convert float type array to Int

**URL:** <https://discourse.julialang.org/t/convert-float-type-array-to-int/65597>\
**Category:** General Usage\
**Tags:** array, type, convert, float\
**Created:** [July 31, 2021, 12:30pm UTC](https://discourse.julialang.org/t/convert-float-type-array-to-int/65597 "2021-07-31T12:30:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![juls](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@juls](https://discourse.julialang.org/u/juls)\
**Post date:** [July 31, 2021, 12:30pm UTC](https://discourse.julialang.org/t/convert-float-type-array-to-int/65597/1 "2021-07-31T12:30:50Z")

</div>

Hi,  
I have been trying to convert the array type to Int for

```julia
a = [3.0 2.0;2.0 4.0;7.0 8.0]

```

I tried convert function and used loops but have not received the required output:

```julia
a = [3 2;2 4;7 8]

```

Thanks

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 31, 2021, 12:31pm UTC](https://discourse.julialang.org/t/convert-float-type-array-to-int/65597/2 "2021-07-31T12:31:57Z")

</div>

```julia
round.(Int, a)

```

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [July 31, 2021, 12:43pm UTC](https://discourse.julialang.org/t/convert-float-type-array-to-int/65597/3 "2021-07-31T12:43:23Z")

</div>

You can use `convert`:

```julia
a = [3.0 2.0;2.0 4.0;7.0 8.0]
b = convert(AbstractArray{Int}, a)

```

Note that you cannot change the element type of an existing array, you must always allocate a new one if you want it to have a different type. But you can of course still use the same variable name for the new array (`a = convert(AbstractArray{Int}, a)`).

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [July 31, 2021, 1:09pm UTC](https://discourse.julialang.org/t/convert-float-type-array-to-int/65597/4 "2021-07-31T13:09:29Z")

</div>

Or

```julia
convert.(Int, a)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 31, 2021, 1:17pm UTC](https://discourse.julialang.org/t/convert-float-type-array-to-int/65597/5 "2021-07-31T13:17:12Z")

</div>

Or maybe just

```julia
Int.(a)

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [July 31, 2021, 7:57pm UTC](https://discourse.julialang.org/t/convert-float-type-array-to-int/65597/6 "2021-07-31T19:57:42Z")

</div>

For completeness, there is also

```julia
julia> trunc.(Int,a)
3×2 Array{Int64,2}:
 3 2
 2 4
 7 8

```

It will always round toward zero.

---

<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:** [July 31, 2021, 8:17pm UTC](https://discourse.julialang.org/t/convert-float-type-array-to-int/65597/7 "2021-07-31T20:17:36Z")

</div>

You have to decide what you want to happen when one of your floats isn’t an integer. What should happen to `[1.0, 2.1]`? Should it round to some integer value or throw an error. The correct solution depends on what you want.
