# Parse 97 or 97.0 to Integer: clever method?

**URL:** <https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406>\
**Category:** New to Julia\
**Tags:** convert, parsing\
**Created:** [January 29, 2022, 1:49am UTC](https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406 "2022-01-29T01:49:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [January 29, 2022, 1:49am UTC](https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406/1 "2022-01-29T01:49:42Z")

</div>

I’m parsing string data from a file written by LabVIEW – numbers written out as strings, which I am parsing to numeric types.

In this example, integers.

The LabVIEW program is a little inconsistent. The pressure settings are always a whole value, but sometimes the LabVIEW program will write out “20” and other times “20.0”

Julia’s built-in parse() will barf on the second one. For example:

```julia
input_press = "20"
setpress = parse(Int,input_press)  
println("setpress = ",setpress)

input_press = "20.0"
setpress = parse(Int,input_press)  
println("setpress = ",setpress)

```

Results in the output:

```julia
setpress = 20

ERROR: ArgumentError: invalid base 10 digit '.' in "20.0"
Stacktrace:

```

**Solution**

I can use the [Parsers](https://github.com/JuliaData/Parsers.jl) package and it’s tryparse() function, which will return [nothing](https://docs.julialang.org/en/v1/base/constants/#Core.nothing) if the conversion fails.

```julia
using Parsers
input_press = "20"
setpress = Parsers.tryparse(Int,input_press)  
if isnothing(setpress)
    floatpress = Parsers.tryparse(Float64,input_press)
    setpress = Integer(floatpress)
end

println("setpress = ",setpress)

input_press = "20.0"
setpress = Parsers.tryparse(Int,input_press)  
if isnothing(setpress)
    floatpress = Parsers.tryparse(Float64,input_press)
    setpress = Integer(floatpress)
end
println("setpress = ",setpress)

```

This works. I could even write it into a small function.

**However**

This being Julia, is there a more _clever_ way to accomplish this?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [January 29, 2022, 1:56am UTC](https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406/2 "2022-01-29T01:56:08Z")

</div>

You could do this, which nicely throws an error if its not actually an integer:

```julia
julia> Int(parse(Float64, "23.0"))
23

julia> Int(parse(Float64, "23"))
23

julia> Int(parse(Float64, "23.1"))
ERROR: InexactError: Int64(23.1)

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 29, 2022, 3:34am UTC](https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406/3 "2022-01-29T03:34:12Z")

</div>

Keep in mind that this method will only be exact for integers up to 2^{53}. Above that, not every `Int64` can be represented as `Float64` anymore.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [January 29, 2022, 4:33am UTC](https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406/4 "2022-01-29T04:33:29Z")

</div>

I don’t think OP needs to worry about exceeding a pressure of 2^53 (in any unit system I’m aware of). Maybe if they’re modeling the Big Bang in Labview?

---

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [January 29, 2022, 5:31am UTC](https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406/5 "2022-01-29T05:31:58Z")

</div>

If you are sure that you can discard the digits after the decimal point (because they are always zeros), you can use `split()` before `parse()`:

```julia-repl
julia> s = "20.0"; parse(Int, split(s, ".")[1])
20

julia> s = "20"; parse(Int, split(s, ".")[1])
20

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [January 29, 2022, 8:31am UTC](https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406/6 "2022-01-29T08:31:58Z")

</div>

`round(Int64, parse(Float64, s))`

---

<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:** [January 29, 2022, 1:04pm UTC](https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406/7 "2022-01-29T13:04:44Z")

</div>

That will silently discard any non-integer part, which doesn’t sound like the desired behavior here:

> [@blackeneth](#):
>
> The pressure settings are always a whole value

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [January 29, 2022, 1:31pm UTC](https://discourse.julialang.org/t/parse-97-or-97-0-to-integer-clever-method/75406/8 "2022-01-29T13:31:42Z")

</div>

Depends on what lies in “are always”. If it’s truly always a whole value it changes nothing.

Of course it depends a lot on the application: is an error better than rounding if a user tries to be funny?
