# Why I can't convert non-integer float to Int64?

**URL:** https://discourse.julialang.org/t/why-i-cant-convert-non-integer-float-to-int64/117276
**Category:** New to Julia
**Tags:** question
**Created:** [July 20, 2024, 2:59pm UTC](https://discourse.julialang.org/t/why-i-cant-convert-non-integer-float-to-int64/117276 "2024-07-20T14:59:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)
#### Post date: [July 20, 2024, 2:59pm UTC](https://discourse.julialang.org/t/why-i-cant-convert-non-integer-float-to-int64/117276/1 "2024-07-20T14:59:37Z")

</div>

Hello, I am new to Julia, I worked in the past mostly in Python and a bit with JAVA, C and C++. My question is that in the code below I get **InexactError**. The conversion works fine with floats with .0 decimal and not with another decimal ? I found that I have to use floor() or other similar functions before conversion.

```julia
v::Float16 = 120.5
Int64(v)

```

The error:

```julia
InexactError: Int64(120.5)

Stacktrace:
 [1] Int64(x::Float16)
   @ Base .\float.jl:912
 [2] top-level scope

```

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [July 20, 2024, 3:02pm UTC](https://discourse.julialang.org/t/why-i-cant-convert-non-integer-float-to-int64/117276/2 "2024-07-20T15:02:18Z")

</div>

The simple answer is that there are many results that you may want when converting the value 120.5 to a decimal and so you need to choose which one you want.

The most obvious choices are `round` (which has several rounding modes) or `ceil`/`floor`

---

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [July 20, 2024, 3:03pm UTC](https://discourse.julialang.org/t/why-i-cant-convert-non-integer-float-to-int64/117276/3 "2024-07-20T15:03:32Z")

</div>

From PEP 20 – The Zen of Python 😄

> Explicit is better than implicit.

---

<div class="post-metadata">

### Author: ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)
#### Post date: [July 20, 2024, 3:05pm UTC](https://discourse.julialang.org/t/why-i-cant-convert-non-integer-float-to-int64/117276/4 "2024-07-20T15:05:00Z")

</div>

Thank you, it make sense yes.

---

<div class="post-metadata">

### Author: ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)
#### Post date: [July 20, 2024, 3:06pm UTC](https://discourse.julialang.org/t/why-i-cant-convert-non-integer-float-to-int64/117276/5 "2024-07-20T15:06:44Z")

</div>

Thank you, yes agreed 😅

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [July 20, 2024, 4:16pm UTC](https://discourse.julialang.org/t/why-i-cant-convert-non-integer-float-to-int64/117276/6 "2024-07-20T16:16:33Z")

</div>

```julia-repl
julia> Float16(120.5)                                   
Float16(120.5)

julia> round(Int64, Float16(120.5))                     
120

julia> round(Int64, Float16(120.5), RoundNearest)       
120

julia> round(Int64, Float16(120.5), RoundNearestTiesAway)
121

```

---

<div class="post-metadata">

### Author: ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)
#### Post date: [July 24, 2024, 12:03pm UTC](https://discourse.julialang.org/t/why-i-cant-convert-non-integer-float-to-int64/117276/7 "2024-07-24T12:03:59Z")

</div>

Thank you.
