Skip to content

Commit

Permalink
convert string into integer
Browse files Browse the repository at this point in the history
  • Loading branch information
xsami committed Jan 5, 2024
1 parent bc8e1c7 commit 4ec7c43
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions StringToInteger/stringToInteger.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Solution for: https://leetcode.com/problems/string-to-integer-atoi/description/
defmodule Solution do
@spec my_atoi(s :: String.t) :: integer
def my_atoi(s) do
s |> String.trim |> Integer.parse |> get_result
end

defp get_result({num, str}) do
if abs(num) >= 2147483648 do
if num > 0 do
2147483647
else
-2147483648
end
else
num
end
end

defp get_result(:error), do: 0

end

0 comments on commit 4ec7c43

Please sign in to comment.