# Converting strings of numbers to numbers?

**URL:** https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164
**Category:** New to Julia
**Created:** [May 26, 2018, 11:59am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164 "2018-05-26T11:59:28Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [May 26, 2018, 11:59am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/1 "2018-05-26T11:59:28Z")

</div>

I have strings of numbers (possibly a vector of strings of numbers) which can be either integer or floating point – I may not know the type in advance – and I want to preserve the type. I understand that I should use function `parse` to convert the string to number. One possible (but ugly?) strategy is the following (e.g., with `s="200"` or `s="2.0e3"`):

```julia
s = "200";
x = try
    parse(Int,s)
  catch
    parse(Float64,s)
end;

```

Is there a better way to do this? Something like `parse(Number,s)`, or something?

---

<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: [May 26, 2018, 12:54pm UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/2 "2018-05-26T12:54:26Z")

</div>

Look at `tryparse`.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [May 26, 2018, 2:16pm UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/3 "2018-05-26T14:16:59Z")

</div>

You can consider parsing as `Float64` and then checking if it is an integer:

```julia
y = parse(Float64, "200")
x = isinteger(y) ? Int(y) : y

```

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [May 26, 2018, 4:21pm UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/4 "2018-05-26T16:21:53Z")

</div>

Thanks for 2 suggestions!

---

<div class="post-metadata">

### Author: ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)
#### Post date: [May 27, 2018, 2:00am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/5 "2018-05-27T02:00:50Z")

</div>

I don’t understand what is wrong with plain old `parse`.

```julia
julia> s1 = "200"
"200"

julia> s2 = "2.0e3"
"2.0e3"

julia> parse(s1)
200

julia> parse(s2)
2000.0

```

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [May 27, 2018, 2:15am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/6 "2018-05-27T02:15:11Z")

</div>

It will no longer work in v0.7/master.  
`parse(string)` is really designed for parsing Julia code, so it has been moved to `Meta.parse`.

Unfortunately, you can’t either do `parse(Number, string)`, or `parse(Unsigned, string)` which would be useful if you don’t mind the type instability, and simply want to get the value returned as a type that can represent the number in the string (if it is a valid format), possibly as a `BigInt` or `BigFloat`.

I may just have to add that functionality to my [Strs](https://github.com/JuliaString/Strs.jl) package 😀

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [May 27, 2018, 2:17am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/7 "2018-05-27T02:17:36Z")

</div>

There is a problem with that though, if the integer would take more than 52/53 bits to represent exactly.

---

<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: [May 27, 2018, 7:11am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/8 "2018-05-27T07:11:28Z")

</div>

> [@JackDevine](#):
>
> I don’t understand what is wrong with plain old `parse` .

1. It invokes the Julia parser, which is a much more generic solution, and consequently less efficient for a specific parsing task. Eg on `v0.6.2`, I find a ~400x speed difference between `parse(...)` and `parse(Int, ...)`.
2. For the same reason, it does not restrict or validate input (as long as it is valid Julia code). This can lead to unintended consequences.

Parsing text as a given type and parsing it as code are very different tasks; consequently `v0.7` distinguishes `Meta.parse`.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [May 27, 2018, 9:57am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/9 "2018-05-27T09:57:15Z")

</div>

I tried to do `parse(Rational,"2//3")` but that didn’t work.  
Anyway, I _assume_ that `parse(Int,string)` and `parse(Float64,string)` will work in v0.7, and that it is `parse(string)` that won’t work in v0.7??

---

<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: [May 27, 2018, 10:12am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/10 "2018-05-27T10:12:16Z")

</div>

Technically `parse(string)` will work in `v0.7`, but you get a deprecation warning.

---

<div class="post-metadata">

### Author: ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)
#### Post date: [May 27, 2018, 10:25am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/11 "2018-05-27T10:25:27Z")

</div>

Just a minor comment. Sorry that’s a bit out of topic.

Would it be better at this point to say "It will no longer work in v1.0”? 😅 So the discussion keeps more focused on what’s the technical problem and not generating confusion between what is v0.7 and v1.0 and so on.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [May 27, 2018, 10:32am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/12 "2018-05-27T10:32:14Z")

</div>

Ok: will `parse(Int,string)` work i v1.0?

---

<div class="post-metadata">

### Author: ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)
#### Post date: [May 27, 2018, 11:48am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/13 "2018-05-27T11:48:53Z")

</div>

Yes, AFAIK what doesn’t show a deprecation warning in v0.7 will work in v1.0 too. What still works in v0.7 but prints a deprecation warning, then you might want to change it to something more stable, since in v1.0 won’t work.

Go for `parse(Int, str)` 😋

---

<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: [May 27, 2018, 11:55am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/14 "2018-05-27T11:55:43Z")

</div>

[Yes](https://github.com/JuliaLang/julia/blob/2256c6baccfbc3ce34956cc72b6d01d3f40904f5/base/parse.jl#L88) (unless, of course, a last minute PR changes that, but this is unlikely 😉)

However, generally, if you are asking questions in the “first steps” category, you should not need to worry about `v0.7` at this point. People are trying to be helpful by pointing these things out, but this is not an immediate concern if you are just learning the language.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [May 27, 2018, 3:14pm UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/15 "2018-05-27T15:14:58Z")

</div>

One possibility would also be to replace `parse(Int,str)` with `Int(str)`, `parse(Float64,str)` with `Float64(str)`, introduce `Rational(str)`, etc. – unless some of these methods are taken.

---

<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: [May 27, 2018, 3:55pm UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/16 "2018-05-27T15:55:54Z")

</div>

Conversion (`convert(T, ...)`), construction (`T(...)`), and parsing (`parse(T, ...)`) are usually distinguished ([the manual](https://docs.julialang.org/en/latest/manual/conversion-and-promotion.html#Conversion-vs.-Construction-1) talks about the first two), even if the distinction isn’t always clearcut. Parsing usually involves ambiguities that have to be resolved/decided (eg the base), and the user should always be prepared for a `ParseError` (or preferably use `tryparse`). Because of this, I like the current arrangement.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [May 27, 2018, 11:40pm UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/17 "2018-05-27T23:40:44Z")

</div>

Might it not be possible to speed up the generic `Meta.parse` method by initially using a regex to check if the string is only digits, or has a decimal point, then branching it to `parse(Int,...)` or whatever? This might help with the 400x performance gap, to at least give it reasonable performance for floats and ints, and one can use the generic algorithm then with better speed.

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [May 28, 2018, 2:38am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/18 "2018-05-28T02:38:55Z")

</div>

For short strings like that, I’m afraid that using Regex might be slower than just some optimized tests for those conditions.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 28, 2018, 9:36am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/19 "2018-05-28T09:36:36Z")

</div>

> [@chakravala](#):
>
> Might it not be possible to speed up the generic `Meta.parse` method by initially using a regex to check if the string is only digits, or has a decimal point, then branching it to `parse(Int,...)` or whatever? This might help with the 400x performance gap, to at least give it reasonable performance for floats and ints, and one can use the generic algorithm then with better speed.

Not really, because the check will fail in all cases where `Meta.parse` is supposed to be used and for the other cases there are already optimized versions. Also, the regex lookup itself will dominate the time so this would still be much slower than using the correct function.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [May 29, 2018, 2:37am UTC](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164/20 "2018-05-29T02:37:18Z")

</div>

The main goal of my post was not to use regexes specifically or to try to replace the specialized methods. There are real world scenarios where completely arbitraty code has to be parsed, and a large percentage of the time you might be dealing with integers or floats, but you might have to be prepared to deal with algebraic expressions and other functional lisp forms. Therefore it is desirable to have a meta-method that selects the optimal parse method upon arbitray code parsing, since input may contain a large percentage of integers or floats, but it is also necessary to be able to handle expressions if necessary.

[Next page](https://discourse.julialang.org/t/converting-strings-of-numbers-to-numbers/11164.md?page=2)
