Computing Ecto virtual field

  1. When you need a computed field, you actually need a function.
  2. Functions are not data, so they should not reside in structs.
  3. If you only want that formatted_sell_price in JSON serialization, you can define your own impl of Jason.Encoder for your Item structs:
defimpl Jason.Encoder, for: Item do
  def encode(%Item{sell_price: price} = item, opts) do
    item
    |> Map.from_struct()
    |> Map.delete(:__meta__)
    |> Map.put(:formatted_sell_price, format_price(price))
    |> Jason.Encode.map(opts)
  end
end

You need to replace Item with your schema module, and format_price with your own function of formatting prices.