# InexactError: Int64

**URL:** <https://discourse.julialang.org/t/inexacterror-int64/115803>\
**Category:** New to Julia\
**Created:** [June 18, 2024, 12:01pm UTC](https://discourse.julialang.org/t/inexacterror-int64/115803 "2024-06-18T12:01:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![evansenvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evansenvy/32/209769_2.png) [@evansenvy](https://discourse.julialang.org/u/evansenvy)\
**Post date:** [June 18, 2024, 12:01pm UTC](https://discourse.julialang.org/t/inexacterror-int64/115803/1 "2024-06-18T12:01:27Z")

</div>

Hi,

I am encountering this error- InexactError: Int64.

`InexactError: Int64(2.5)`

When I search for this I got this article [unrelated blogspam link redacted] and as per this, explaining an answer in which they are talking about the value being converted can be exactly represented as an integer, or handle the conversion appropriately. And on the other hand, this resource [ERROR: LoadError: InexactError: Int64(Int64, 7.5)](https://discourse.julialang.org/t/error-loaderror-inexacterror-int64-int64-7-5/74046).

However I fixed the value issue but how to fix the floating-point number, because it’s must.

```julia
x = 2.5
y = Int64(x) # This will cause an InexactError

# Proper handling
y = round(Int64, x)

```

For the above code, I converted the valve in integer. But my priority is to use floating-point number.

How can I fix them?

Thanks  
Evan

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [June 18, 2024, 12:21pm UTC](https://discourse.julialang.org/t/inexacterror-int64/115803/2 "2024-06-18T12:21:35Z")

</div>

When `x` cannot be exactly represented as an integer, `Int64(x)` will raise an `InexactError`.

```julia
Int64(2.0) # works, equals to integer 2
Int64(2.1) # raises InexactError because 2.1 is not an integer

```

I don’t understand the last part of your post. If your priority is to use a floating-point number, then why would you call `Int64` at all? Maybe your goal is to round the number to the nearest integer but keep the floating-point type? In that case, use just `round` without the first argument:

```julia
x = 2.1
y = round(x) # equals to float 2.0

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 18, 2024, 12:54pm UTC](https://discourse.julialang.org/t/inexacterror-int64/115803/3 "2024-06-18T12:54:12Z")

</div>

> [@barucden](#):
>
> If your priority is to use a floating-point number, then why would you call `Int64` at all?

The most common situation for an error like this is that someone accidentally constructed an array of integers and then tried to store a floating-point number, e.g.

```julia
julia> x = [1,2,3,4]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> x[2] = 3.5
ERROR: InexactError: Int64(3.5)

```

The solution is to make the array a container of floating-point numbers to start with, e.g. via `x = Float64[1,2,3,4]` or `x = [1.0,2,3,4]`.
