title: Elixir - Convert a string number or empty string to a float or nil
body: <p>I am trying to convert the <code>price</code> field, which is a string (eg <code>"2.22"</code> or <code>""</code>) to a float or nil, and then add it to the database.</p>
  
  <pre><code>  def insert_product_shop(conn, product_id, shop_id, price) do
      priceFloat = nil
      if price not in [""] do
        price = elem(Float.parse(price), 0)
        priceFloat = price / 1
        IO.inspect(priceFloat)
      else
        priceFloat = nil
      end
      IO.inspect(priceFloat)
      changeset = Api.ProductShop.changeset(%Api.ProductShop{
        p_id: product_id,
        s_id: shop_id,
        price: priceFloat,
        not_in_shop_count: 0,
        is_in_shop_count: 0
        })
      errors = changeset.errors
      valid = changeset.valid?
      IO.inspect(changeset)
      case insert(changeset) do
        {:ok, product_shop} -&gt;
          {:ok, product_shop}
        {:error, changeset} -&gt;
          {:error, :failure}
      end
    end
  </code></pre>
  
  <p>the output is:</p>
  
  <pre><code>2.22
  nil
  #Ecto.Changeset&lt;action: nil, changes: %{}, errors: [], data: #Api.ProductShop&lt;&gt;,
   valid?: true&gt;
  
  13:25:41.745 [debug] QUERY OK db=2.0ms
  INSERT INTO "product_shops" ("is_in_shop_count","not_in_shop_count","p_id","s_id") VALUES ($1,$2,$3,$4) RETURNING "id" [0, 0, 40, 1]
  </code></pre>
  
  <p>As the output shows, <code>priceFloat</code> becomes nil, I assume because when I set it to 2.22 it was out of scope. Maybe my code is too imperative. How can I rewrite this to convert "2.22" to 2.22 without making it nil, and allow "" to be converted to nil?</p>