# Demoting BigInt to Int64

**URL:** <https://discourse.julialang.org/t/demoting-bigint-to-int64/52968>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 7, 2021, 12:28am UTC](https://discourse.julialang.org/t/demoting-bigint-to-int64/52968 "2021-01-07T00:28:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [January 7, 2021, 12:28am UTC](https://discourse.julialang.org/t/demoting-bigint-to-int64/52968/1 "2021-01-07T00:28:46Z")

</div>

I need to read some numbers from a file and then do things with them. In theory I may get numbers that are bigger than 2^{128} so I parse them as `BigInt`. However, usually they are smaller than 2^{63} so `Int64` is enough.

My “solution” was to parse them as `BigInt` and then demote them to `Int64` if they are not too big. Then I can create my structure and go do my thing. The problem with this is that `parse` does not want to change `BigInt` to `Int64`. This is reasonable since it will usually fail, but in this case it won’t. Is there any way to do that or I have to parse them as `Int64` and start catching errors?

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [January 7, 2021, 12:35am UTC](https://discourse.julialang.org/t/demoting-bigint-to-int64/52968/2 "2021-01-07T00:35:00Z")

</div>

Instead of catching errors, I would suggest using `tryparse`.

[https://docs.julialang.org/en/v1/base/numbers/#Base.tryparse](https://docs.julialang.org/en/v1/base/numbers/#Base.tryparse)

---

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [January 7, 2021, 12:44am UTC](https://discourse.julialang.org/t/demoting-bigint-to-int64/52968/3 "2021-01-07T00:44:42Z")

</div>

I just realised that the easiest fix is to parse them twice. I can parse them once as BigInt, check how big they are and parse them again with the correct type. 🙂

---

<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:** [January 7, 2021, 12:54am UTC](https://discourse.julialang.org/t/demoting-bigint-to-int64/52968/4 "2021-01-07T00:54:23Z")

</div>

can’t you do something like:

```julia
# a is BigInt

a = a<=typemax(Int64) ? Int64(a) : a

```

---

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [January 7, 2021, 1:03am UTC](https://discourse.julialang.org/t/demoting-bigint-to-int64/52968/5 "2021-01-07T01:03:15Z")

</div>

Doesn’t this call `parse`?

EDIT: It seems it doesn’t. I just tried it. I thought that all type conversions were done through `parse`.

---

<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:** [January 7, 2021, 1:09am UTC](https://discourse.julialang.org/t/demoting-bigint-to-int64/52968/6 "2021-01-07T01:09:15Z")

</div>

no, `parse` is used to ‘parse’ literal “text” into numerical bits. Once you have the number, you can do computer-stuff

---

<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:** [January 7, 2021, 9:06am UTC](https://discourse.julialang.org/t/demoting-bigint-to-int64/52968/7 "2021-01-07T09:06:41Z")

</div>

It’s really better to use `tryparse(Int, a)`, and parse to `BigInt` only if that returns `nothing`. Parsing to `BigInt` is _much_ slower, so go for `tryparse`.

> [@jling](#):
>
> `a = a<=typemax(Int64) ? Int64(a) : a`

Should also compare to `typemin(Int)`, but no need for this anyway, if using `tryparse(Int, ...)`
