# How can I use elm/parser to make a Float from a String?

**URL:** <https://discourse.elm-lang.org/t/how-can-i-use-elm-parser-to-make-a-float-from-a-string/3754>\
**Category:** Learn\
**Created:** [June 6, 2019, 1:32am UTC](https://discourse.elm-lang.org/t/how-can-i-use-elm-parser-to-make-a-float-from-a-string/3754 "2019-06-06T01:32:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kochab](https://avatars.discourse-cdn.com/v4/letter/k/a88e57/32.png) [@Kochab](https://discourse.elm-lang.org/u/Kochab)\
**Post date:** [June 6, 2019, 1:32am UTC](https://discourse.elm-lang.org/t/how-can-i-use-elm-parser-to-make-a-float-from-a-string/3754/1 "2019-06-06T01:32:52Z")

</div>

I have a string that looks like this `4.6.178.3429`  
Can someone explain how I would use `elm/parser` to turn this into a float that looks like this `4.6`?

---

<div class="post-metadata">

**Author:** ![Lucas\_Payr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lucas_payr/32/4993_2.png) [@Lucas\_Payr](https://discourse.elm-lang.org/u/Lucas_Payr)\
**Post date:** [June 6, 2019, 4:53am UTC](https://discourse.elm-lang.org/t/how-can-i-use-elm-parser-to-make-a-float-from-a-string/3754/2 "2019-06-06T04:53:46Z")

</div>

Essentially, your code should look something like this:

```elm
test : String -> Maybe Float
test string = 
    string
    |> Parser.run Parser.float
    |> Result.toMaybe

```

Here are some results:

```auto
"4.6.178.3429" -> Just 4.6
"4.6test.178.3429" -> Just 4.6
"4test.6.178.3429" -> Just 4
"test4.6.178.3429" -> Nothing

```

Checkout the talk [Demystifying Parsers](https://www.youtube.com/watch?v=M9ulswr1z0E) by Tereza Sokol for a detailed explanation of elm/parser.  
I you don’t want to watch the full video, checkout [this summary](https://orasund.gitbook.io/elm-cookbook/frameworks-1/elm-parser).

---

<div class="post-metadata">

**Author:** ![Kochab](https://avatars.discourse-cdn.com/v4/letter/k/a88e57/32.png) [@Kochab](https://discourse.elm-lang.org/u/Kochab)\
**Post date:** [June 7, 2019, 3:28am UTC](https://discourse.elm-lang.org/t/how-can-i-use-elm-parser-to-make-a-float-from-a-string/3754/3 "2019-06-07T03:28:16Z")

</div>

So I just realized this solution is not going to work. I’m trying to parse a version number so 4.100 is greater than 4.1, but of course a float does not care about trailing zeros so 4.100 turns into 4.1. My attempt at writing my own parser to handle each part (major, minor, patch, millipatch) has ended in failure.

I think I need some combination of `chompWhile Char.isDigit`, `getChompedString`, some way of turning that chomped string into an int, throwing out the “.”, and doing it all again, but how to accomplish that isn’t clear to me.

Would you mind helping me out some more? I have an Ellie happening: [https://ellie-app.com/5L3vY6QCq4ka1](https://ellie-app.com/5L3vY6QCq4ka1)

---

<div class="post-metadata">

**Author:** ![Lucas\_Payr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lucas_payr/32/4993_2.png) [@Lucas\_Payr](https://discourse.elm-lang.org/u/Lucas_Payr)\
**Post date:** [June 7, 2019, 5:29am UTC](https://discourse.elm-lang.org/t/how-can-i-use-elm-parser-to-make-a-float-from-a-string/3754/4 "2019-06-07T05:29:22Z")

</div>

Sure.  
But I’m not quite sure what you actually try to archive.  
Could you maybe give me some inputs and the expected output?

Your ellie looks fine to me, only the `Result.withDefault` confuses me -\> whats the problem with `Result.toMaybe`?

**Edit:**  
Just realized that your ellie does wierd things. Let me see if i can fix it.

---

<div class="post-metadata">

**Author:** ![Lucas\_Payr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lucas_payr/32/4993_2.png) [@Lucas\_Payr](https://discourse.elm-lang.org/u/Lucas_Payr)\
**Post date:** [June 7, 2019, 6:02am UTC](https://discourse.elm-lang.org/t/how-can-i-use-elm-parser-to-make-a-float-from-a-string/3754/5 "2019-06-07T06:02:03Z")

</div>

This should work:

[https://ellie-app.com/5L5WB9qcsJ9a1](https://ellie-app.com/5L5WB9qcsJ9a1)

---

<div class="post-metadata">

**Author:** ![Kochab](https://avatars.discourse-cdn.com/v4/letter/k/a88e57/32.png) [@Kochab](https://discourse.elm-lang.org/u/Kochab)\
**Post date:** [June 7, 2019, 1:00pm UTC](https://discourse.elm-lang.org/t/how-can-i-use-elm-parser-to-make-a-float-from-a-string/3754/6 "2019-06-07T13:00:15Z")

</div>

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [June 17, 2019, 1:06pm UTC](https://discourse.elm-lang.org/t/how-can-i-use-elm-parser-to-make-a-float-from-a-string/3754/7 "2019-06-17T13:06:00Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
