# Parsing big integer as string

**URL:** <https://discourse.julialang.org/t/parsing-big-integer-as-string/65135>\
**Category:** New to Julia\
**Created:** [July 22, 2021, 7:03pm UTC](https://discourse.julialang.org/t/parsing-big-integer-as-string/65135 "2021-07-22T19:03:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daviddoij](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daviddoij](https://discourse.julialang.org/u/daviddoij)\
**Post date:** [July 22, 2021, 7:03pm UTC](https://discourse.julialang.org/t/parsing-big-integer-as-string/65135/1 "2021-07-22T19:03:57Z")

</div>

Hi there,

I’m trying to solve project euler #13 in julia. I have to give the first 10 digits of a humongous integer.  
To obtain that integer, I have to read line by line a file with really big integers and summing them up. The solution will be the first ten digits of that integer.

I could sum them up but I’m getting troubles when parsing it. This part reads the file and calculate the result as a float:

```julia
using DoubleFloats
result = sum(parse.(Double64, readlines("my_file.txt")))

```

but the result is printed as “5.5373762303908766373020487468328705e+51”. If I try to parse it to a “BigInt” I get the following error:

`ERROR: LoadError: MethodError: no method matching parse(::Type{BigInt}, ::Double64)`

How can I get the first 10 digits of that big integer?

Thanks in advance

\*“my\_file.txt” has 100 big integers such as this one: 37107287533902102798797998220837590246510135740250

Each of these integers is in a new line without \n to separate them.

---

<div class="post-metadata">

**Author:** ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)\
**Post date:** [July 22, 2021, 7:26pm UTC](https://discourse.julialang.org/t/parsing-big-integer-as-string/65135/2 "2021-07-22T19:26:50Z")

</div>

can you give an example of what code you are running to generate the error? I assume it is one of either  
`parse(Double64,result)` or `result = sum(parse.(BigInt,readlines("my_file.txt")))` or maybe something else but it is unclear what.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 22, 2021, 7:35pm UTC](https://discourse.julialang.org/t/parsing-big-integer-as-string/65135/3 "2021-07-22T19:35:29Z")

</div>

```julia
nbs = map(x->parse(BigInt,x),readlines(raw"C:\temp\data.txt"))
res = sum(nbs)
string(res)[1:10]

```

---

<div class="post-metadata">

**Author:** ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)\
**Post date:** [July 22, 2021, 7:37pm UTC](https://discourse.julialang.org/t/parsing-big-integer-as-string/65135/4 "2021-07-22T19:37:40Z")

</div>

or as a one liner I believe that  
`string(sum(parse.(BigInt,readlines("my_file.txt"))))[1:10]`  
should work…I think.

---

<div class="post-metadata">

**Author:** ![daviddoij](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daviddoij](https://discourse.julialang.org/u/daviddoij)\
**Post date:** [July 22, 2021, 7:52pm UTC](https://discourse.julialang.org/t/parsing-big-integer-as-string/65135/5 "2021-07-22T19:52:28Z")

</div>

> [@bernhard](#):
>
> ```julia
> nbs = map(x->parse(BigInt,x),readlines(raw"C:\temp\data.txt"))
> res = sum(nbs)
> string(res)[1:10]
> 
> ```

Sorry for not being more concrete. The code that generates the error is:

```julia
parse(Double64,result)

```

---

<div class="post-metadata">

**Author:** ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)\
**Post date:** [July 22, 2021, 7:57pm UTC](https://discourse.julialang.org/t/parsing-big-integer-as-string/65135/6 "2021-07-22T19:57:51Z")

</div>

“result” isn’t a string so there is no need to parse it. (Parsing is basically for converting a string into some other datatype. Instead you need to convert `result` (which in your original example is a Double64) to a BigInt. This is performed by doing `BigInt(result)`.

However as both @bernhard and I have shown there is no particular reason to use Double64, you can parse directly to BigInt as shown (I think, I was too lazy to create a demo file to try for sure but it should work).

---

<div class="post-metadata">

**Author:** ![daviddoij](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daviddoij](https://discourse.julialang.org/u/daviddoij)\
**Post date:** [July 22, 2021, 8:01pm UTC](https://discourse.julialang.org/t/parsing-big-integer-as-string/65135/7 "2021-07-22T20:01:16Z")

</div>

Thx for the explanation. I’m learning lots of new things everyday 😃
