# Type-safe conversion of strings to numbers

**URL:** <https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812>\
**Category:** General Usage\
**Tags:** strings, float\
**Created:** [November 5, 2023, 6:46am UTC](https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812 "2023-11-05T06:46:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![janonime](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janonime/32/202344_2.png) [@janonime](https://discourse.julialang.org/u/janonime)\
**Post date:** [November 5, 2023, 6:46am UTC](https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812/1 "2023-11-05T06:46:43Z")

</div>

It is easy to convert a string to a Float64:

```julia
julia> parse(Float64,"1.23456789")
1.23456789

```

It is (obviously) not possible to convert a string to an AbstractFloat:

```julia
julia> parse(AbstractFloat,"1.23456789")
ERROR: MethodError: no method matching tryparse(::Type{AbstractFloat}, ::String)

```

Is it possible to convert strings to numbers in a type-safe way?

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [November 5, 2023, 6:53am UTC](https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812/2 "2023-11-05T06:53:38Z")

</div>

Use `tryparse`:

```julia
julia> tryparse(Int, "a") === nothing
true

julia> tryparse(Int, "12")
12

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 5, 2023, 7:45am UTC](https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812/3 "2023-11-05T07:45:53Z")

</div>

For a result that isn’t `nothing` or an error, you really do need a specific type. If you don’t want to specify a type at all, you could fall back to parsing in the source code sense `eval(Meta.parse("1im")`, though the evaluation occurs in the global scope (for example, if you do the call in a function, execution moves to the global scope then provides the value to the function).

---

<div class="post-metadata">

**Author:** ![janonime](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janonime/32/202344_2.png) [@janonime](https://discourse.julialang.org/u/janonime)\
**Post date:** [November 5, 2023, 8:22am UTC](https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812/4 "2023-11-05T08:22:39Z")

</div>

Thanks! So if I use `eval(Meta.parse("1.23456789"))`, then my package will work regardless of whether it is installed on a 32 or 64 bit system? It does seem strange to me that type-safe string conversion is straightforward for Integers (`Int` is equivalent to `Int32` on a 32-bit system), but not for floats. Is there any logical reason for that?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 5, 2023, 8:38am UTC](https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812/5 "2023-11-05T08:38:26Z")

</div>

`Int` is a `const`ant alias for `Int32` on 32-bit systems or `Int64` on 64-bit systems. `Integer` is the abstract type for integers, analogous to `AbstractFloat` for floating points.

I don’t know your use case but you should avoid manually calling the parse-evaluate process, running things in the global scope isn’t cheap. Though if you really are reading in strings with arbitrary Julia code, that might be necessary and it would be unfeasible to try to narrow down types at compile-time.

---

<div class="post-metadata">

**Author:** ![janonime](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janonime/32/202344_2.png) [@janonime](https://discourse.julialang.org/u/janonime)\
**Post date:** [November 5, 2023, 9:37am UTC](https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812/6 "2023-11-05T09:37:55Z")

</div>

Okay. In that case the cleanest solution would probably be to define my own `const`ant alias, like this:

```julia
julia> const Float = Sys.WORD_SIZE==64 ? Float64 : Float32
julia> parse(Float,"1.23456789")
1.23456789

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [November 5, 2023, 9:48am UTC](https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812/7 "2023-11-05T09:48:15Z")

</div>

No, `Float64` vs `Float32` has nothing to do with the word size, they are unrelated concepts. Just use `Float64`.

Floating point arithmetic is handled by separate circuits on the processor from integer arithmetic, and even on a machine with a 32-bit address space, specific hardware for 64 bit floating point arithmetic has been standard for decades. 64 bit address spaces are **way** newer than 64 bit float support (because 32 bit floats are incredibly restrictive).

The only machines old enough to not have native hardware support for `Float64` are too ancient to actually compile and run julia code anyways.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 5, 2023, 10:00am UTC](https://discourse.julialang.org/t/type-safe-conversion-of-strings-to-numbers/105812/8 "2023-11-05T10:00:08Z")

</div>

There’s no need, decimals or `e`-notation do `Float64` and `f`-notation does `Float32` on both systems. `Int` and `UInt` are documented examples of [Type Aliases](https://docs.julialang.org/en/v1/manual/types/#Type-Aliases) and this is explained:

> (Note that unlike `Int` , `Float` does not exist as a type alias for a specific sized [`AbstractFloat`](https://docs.julialang.org/en/v1/base/numbers/#Core.AbstractFloat). Unlike with integer registers, where the size of `Int` reflects the size of a native pointer on that machine, the floating point register sizes are specified by the IEEE-754 standard.)
