# Parse Float32 string

**URL:** <https://discourse.julialang.org/t/parse-float32-string/103667>\
**Category:** General Usage\
**Created:** [September 8, 2023, 1:44pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667 "2023-09-08T13:44:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnbb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnbb/32/34233_2.png) [@johnbb](https://discourse.julialang.org/u/johnbb)\
**Post date:** [September 8, 2023, 1:44pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/1 "2023-09-08T13:44:38Z")

</div>

How can I parse strings like “1.23f-4” to Float32?

```julia
julia> parse(Float64, "1.23e-4")
0.000123

julia> parse(Float32, "1.23f-4")
ERROR: ArgumentError: cannot parse "1.23f-4" as Float32
Stacktrace:
 [1] _parse_failure(T::Type, s::String, startpos::Int64, endpos::Int64)
   @ Base ./parse.jl:373
 [2] _parse_failure(T::Type, s::String)
   @ Base ./parse.jl:373
 [3] #tryparse_internal#509
   @ ./parse.jl:369 [inlined]
 [4] tryparse_internal
   @ ./parse.jl:366 [inlined]
 [5] #parse#510
   @ ./parse.jl:379 [inlined]
 [6] parse(::Type{Float32}, s::String)
   @ Base ./parse.jl:379
 [7] top-level scope
   @ REPL[87]:1

```

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [September 8, 2023, 1:53pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/2 "2023-09-08T13:53:54Z")

</div>

> [@johnbb](#):
>
> `parse(Float32, "1.23f-4")`

Did you mean `parse(Float32, "1.23e-4")`? This works.

---

<div class="post-metadata">

**Author:** ![johnbb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnbb/32/34233_2.png) [@johnbb](https://discourse.julialang.org/u/johnbb)\
**Post date:** [September 8, 2023, 1:55pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/3 "2023-09-08T13:55:30Z")

</div>

No. The strings are like “1.23f-4”.

---

<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:** [September 8, 2023, 2:01pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/4 "2023-09-08T14:01:59Z")

</div>

See [Parsing Float32 literals](https://discourse.julialang.org/t/parsing-flot32-literals/25204) and [Extracting a float from a string](https://discourse.julialang.org/t/extracting-a-float-from-a-string/43126)

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [September 8, 2023, 2:03pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/5 "2023-09-08T14:03:29Z")

</div>

What does that notation mean by the way? What decimal number corresponds to `1.23f-4`?

Edit: Ugh, I was not aware:

```julia
julia> 1.23f-4
0.000123f0

```

---

<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:** [September 8, 2023, 2:08pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/6 "2023-09-08T14:08:17Z")

</div>

[This comment](https://github.com/JuliaLang/julia/pull/44910#issuecomment-1110160383) is informative on why `Base.parse` doesn’t do what `Meta.parse` can, I’ll just copy the text too:

> In general, `parse(T, string)` isn’t related to Julia syntax. `parse(Int, "1_000")` doesn’t work either, even though `1_000` is a valid Julia integer literal, whereas `parse(DateTime, "2022-04-26T15:15")` works even though `2022-04-26T15:15` is not Julia syntax. What we accept in `parse` is more about what we’re likely to find in external data sources (e.g. CSV files).

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 8, 2023, 2:08pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/7 "2023-09-08T14:08:43Z")

</div>

> [@barucden](#):
>
> Edit: Ugh, I was not aware:
> 
> ```julia
> julia> 1.23f-4
> 0.000123f0
> 
> ```

Note that the returned object is a `Float32`, not a `Float64`, which is what the OP is asking about - Parsing a Float32 formatted as a string representing as a Float64.

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [September 8, 2023, 2:09pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/8 "2023-09-08T14:09:18Z")

</div>

There is a related [discussion](https://github.com/JuliaLang/julia/issues/5690) on github, and your issue seemingly still does not work out of the box.  
You could use, e.g., package [Parsers.jl](https://github.com/JuliaData/Parsers.jl) which does the job.

```julia
julia> x = Parsers.parse(Float32, "1.23f-4")
0.000123f0

```

---

<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:** [September 8, 2023, 2:30pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/9 "2023-09-08T14:30:51Z")

</div>

> [@johnbb](#):
>
> How can I parse strings like “1.23f-4” to Float32?

Out of curiosity, where are you seeing data (not source code) with this format? It’s nonstandard for e.g. CSV files.

---

<div class="post-metadata">

**Author:** ![johnbb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnbb/32/34233_2.png) [@johnbb](https://discourse.julialang.org/u/johnbb)\
**Post date:** [September 8, 2023, 2:46pm UTC](https://discourse.julialang.org/t/parse-float32-string/103667/10 "2023-09-08T14:46:05Z")

</div>

I am filtering log files created by a job running Julia code. Float32 is used for Flux. @stevengj Thanks for quick response.
