# Convert floating point to integer type?

**URL:** <https://discourse.julialang.org/t/convert-floating-point-to-integer-type/103273>\
**Category:** General Usage\
**Tags:** numbers\
**Created:** [August 28, 2023, 5:57am UTC](https://discourse.julialang.org/t/convert-floating-point-to-integer-type/103273 "2023-08-28T05:57:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [August 28, 2023, 5:57am UTC](https://discourse.julialang.org/t/convert-floating-point-to-integer-type/103273/1 "2023-08-28T05:57:17Z")

</div>

Currently, the `float` function allows one to go from an `Integer` type to a floating-point type.

```julia
julia> float(BigInt)
BigFloat

```

Is there a function to go the other way, that is given `BigFloat` as an argument, it returns `BigInt`, and similarly for other types?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 28, 2023, 5:59am UTC](https://discourse.julialang.org/t/convert-floating-point-to-integer-type/103273/2 "2023-08-28T05:59:07Z")

</div>

> [@jishnub](#):
>
> there a function to go the other way, that is given `BigFloat` as an argument, it returns `BigInt`, and similarly for other types?

there is `Base.inttype`, but it seems internal and only has definitions for `Float16/32/64`

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 28, 2023, 7:13am UTC](https://discourse.julialang.org/t/convert-floating-point-to-integer-type/103273/3 "2023-08-28T07:13:36Z")

</div>

> [@jishnub](#):
>
> Is there a function to go the other way, that is given `BigFloat` as an argument, it returns `BigInt`, and similarly for other types?

What properties do you expect from such a mapping? When you go from integers to floats, the expectation is that the float type will have a wider range than the integer, so conversion is possible (at the price of losing precision).

Going in the other direction, this is impractical because floats have such a wide range.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [August 28, 2023, 7:58am UTC](https://discourse.julialang.org/t/convert-floating-point-to-integer-type/103273/4 "2023-08-28T07:58:13Z")

</div>

I was mainly thinking of a map from integers to floats (in the context of axes of arrays), so that given a specific `BigFloat` range, I may choose the axis to be a `BigInt` range. I wasn’t thinking of conversion.

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [August 28, 2023, 9:39am UTC](https://discourse.julialang.org/t/convert-floating-point-to-integer-type/103273/5 "2023-08-28T09:39:49Z")

</div>

For going from float to integer I normally use `round(T, x)`:

```julia
julia> round(Int64, 3.14159)
3

```

It throws an error if the `Float64` value is out of range for an `Int64`:

```julia
julia> round(Int64, 9e23)
ERROR: InexactError: trunc(Int64, 9.0e23)
Stacktrace:
 [1] trunc
   @ Base ./float.jl:902 [inlined]
 [2] round(::Type{Int64}, x::Float64)
   @ Base ./float.jl:385
 [3] top-level scope
   @ REPL[24]:1

```

It also works with `BigFloat`/`BigInt`:

```julia
julia> x=round(BigInt, BigFloat(1.33))
1

julia> typeof(x)
BigInt

```

I have not tested whether an error is thrown if the `BigFloat` value is out of range for `BigInt`, but probably yes.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 28, 2023, 10:43am UTC](https://discourse.julialang.org/t/convert-floating-point-to-integer-type/103273/6 "2023-08-28T10:43:45Z")

</div>

> [@jishnub](#):
>
> I may choose the axis to be a `BigInt` range. I wasn’t thinking of conversion.

I think your best option is define your own function for this, as the API in `Base` is mainly for conversion / promotion and their fallbacks.

---

<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:** [August 28, 2023, 12:34pm UTC](https://discourse.julialang.org/t/convert-floating-point-to-integer-type/103273/7 "2023-08-28T12:34:06Z")

</div>

> [@jishnub](#):
>
> Is there a function to go the other way, that is given `BigFloat` as an argument, it returns `BigInt`, and similarly for other types?

Use the abstract type `Integer`:

```julia
julia> Integer(3.0)
3

julia> Integer(3.0) |> typeof
Int64

julia> Integer(big"3.0")
3

julia> Integer(big"3.0") |> typeof
BigInt

```

Of course, this only works if the floating-point type is integer-valued. If you want an approximate conversion, then you need to specify an approximation explicitly, e.g. via `round`:

```julia
julia> round(Integer, 3.1) |> typeof
Int64

julia> round(Integer, big"3.1") |> typeof
BigInt

```
