diff --git a/lib/microwaveprop/beacons/beacon.ex b/lib/microwaveprop/beacons/beacon.ex index 27264f33..dc74be5d 100644 --- a/lib/microwaveprop/beacons/beacon.ex +++ b/lib/microwaveprop/beacons/beacon.ex @@ -52,11 +52,18 @@ defmodule Microwaveprop.Beacons.Beacon do """ def format_mw(nil), do: "" - def format_mw(mw) when is_float(mw) do - if mw == trunc(mw) do - Integer.to_string(trunc(mw)) + def format_mw(mw) when is_number(mw) do + int_part = trunc(mw) + frac = mw - int_part + + formatted_int = add_commas(int_part) + + if frac == 0.0 do + formatted_int else - mw |> :erlang.float_to_binary(decimals: 3) |> trim_trailing_zeros() + decimal = mw |> :erlang.float_to_binary(decimals: 3) |> trim_trailing_zeros() + [_int, frac_part] = String.split(decimal, ".") + "#{formatted_int}.#{frac_part}" end end @@ -69,13 +76,7 @@ defmodule Microwaveprop.Beacons.Beacon do int_part = trunc(mhz) frac = mhz - int_part - formatted_int = - int_part - |> Integer.to_string() - |> String.reverse() - |> String.replace(~r/.{3}/, "\\0,") - |> String.trim_trailing(",") - |> String.reverse() + formatted_int = add_commas(int_part) if frac == 0.0 do formatted_int @@ -88,6 +89,15 @@ defmodule Microwaveprop.Beacons.Beacon do def format_freq(mhz), do: to_string(mhz) + defp add_commas(int) do + int + |> Integer.to_string() + |> String.reverse() + |> String.replace(~r/.{3}/, "\\0,") + |> String.trim_trailing(",") + |> String.reverse() + end + defp trim_trailing_zeros(str) do if String.contains?(str, ".") do str |> String.trim_trailing("0") |> String.trim_trailing(".") @@ -217,8 +227,8 @@ defmodule Microwaveprop.Beacons.Beacon do case Maidenhead.to_latlon(grid) do {:ok, {new_lat, new_lon}} -> changeset - |> put_change(:lat, new_lat) - |> put_change(:lon, new_lon) + |> put_change(:lat, Float.round(new_lat * 1.0, 6)) + |> put_change(:lon, Float.round(new_lon * 1.0, 6)) :error -> changeset diff --git a/lib/microwaveprop_web/live/beacon_live/index.ex b/lib/microwaveprop_web/live/beacon_live/index.ex index 2c9bea74..fb18aacc 100644 --- a/lib/microwaveprop_web/live/beacon_live/index.ex +++ b/lib/microwaveprop_web/live/beacon_live/index.ex @@ -27,8 +27,8 @@ defmodule MicrowavepropWeb.BeaconLive.Index do <:col :let={{_id, beacon}} label="Frequency (MHz)">{Beacon.format_freq(beacon.frequency_mhz)} <:col :let={{_id, beacon}} label="Call">{beacon.callsign} <:col :let={{_id, beacon}} label="Grid">{beacon.grid} - <:col :let={{_id, beacon}} label="Lat">{beacon.lat} - <:col :let={{_id, beacon}} label="Lon">{beacon.lon} + <:col :let={{_id, beacon}} label="Lat">{format_coord(beacon.lat)} + <:col :let={{_id, beacon}} label="Lon">{format_coord(beacon.lon)} <:col :let={{_id, beacon}} label="EIRP (mW)">{Beacon.format_mw(beacon.power_mw)} <:col :let={{_id, beacon}} label="Height AGL (ft)">{beacon.height_ft} <:col :let={{_id, beacon}} label="Keying">{Beacon.keying_label(beacon.keying)} @@ -162,4 +162,7 @@ defmodule MicrowavepropWeb.BeaconLive.Index do defp admin?(%{user: %{is_admin: true}}), do: true defp admin?(_), do: false + + defp format_coord(value) when is_float(value), do: :erlang.float_to_binary(value, decimals: 6) + defp format_coord(value), do: to_string(value) end diff --git a/priv/repo/migrations/20260411144719_round_beacon_lat_lon_to_6_decimals.exs b/priv/repo/migrations/20260411144719_round_beacon_lat_lon_to_6_decimals.exs new file mode 100644 index 00000000..e903639e --- /dev/null +++ b/priv/repo/migrations/20260411144719_round_beacon_lat_lon_to_6_decimals.exs @@ -0,0 +1,9 @@ +defmodule Microwaveprop.Repo.Migrations.RoundBeaconLatLonTo6Decimals do + use Ecto.Migration + + def up do + execute "UPDATE beacons SET lat = ROUND(lat::numeric, 6), lon = ROUND(lon::numeric, 6)" + end + + def down, do: :ok +end