defmodule Gulib.Float do def ceiling(float) do t = trunc(float) case float - t do neg when neg < 0 -> t pos when pos > 0 -> t + 1 _ -> t end end def to_binary(value, opts \\ []) do :erlang.float_to_binary(value, opts) end def parse(x) when is_nil(x), do: 0.0 def parse(x) when is_float(x), do: x def parse(x) when is_integer(x) do "#{x}.0" |> String.to_float end def parse(x) when is_binary(x) or is_bitstring(x) do try do x = x |> String.trim negative = x |> String.starts_with?("-") x = if negative do String.trim_leading(x, "-") else x end |> String.split(~r{[^0-9.]}) |> List.first |> String.split(".") |> Enum.filter(fn(i) -> i != "" end) value = "#{Enum.at(x, 0)}.#{Enum.at(x, 1) || 0}" |> String.to_float if negative, do: -value, else: value rescue ArgumentError -> 0.0 end end def parse(x) when is_atom(x) do try do x |> Atom.to_string |> parse rescue ArgumentError -> 0.0 end end end