Round lat/lon to 6 decimals everywhere, add commas to EIRP display

- Round grid-derived lat/lon in maybe_fill_latlon changeset step
- Format lat/lon to 6 decimal places on index table
- Migrate existing beacon data to 6 decimal precision
- Add comma separators to EIRP mW display (e.g. 10,000 mW)
- Extract shared add_commas helper for format_freq and format_mw
This commit is contained in:
Graham McIntire 2026-04-11 09:49:11 -05:00
parent 1a46020f6a
commit b5162daf89
3 changed files with 37 additions and 15 deletions

View file

@ -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

View file

@ -27,8 +27,8 @@ defmodule MicrowavepropWeb.BeaconLive.Index do
<:col :let={{_id, beacon}} label="Frequency (MHz)">{Beacon.format_freq(beacon.frequency_mhz)}</:col>
<:col :let={{_id, beacon}} label="Call">{beacon.callsign}</:col>
<:col :let={{_id, beacon}} label="Grid">{beacon.grid}</:col>
<:col :let={{_id, beacon}} label="Lat">{beacon.lat}</:col>
<:col :let={{_id, beacon}} label="Lon">{beacon.lon}</:col>
<:col :let={{_id, beacon}} label="Lat">{format_coord(beacon.lat)}</:col>
<:col :let={{_id, beacon}} label="Lon">{format_coord(beacon.lon)}</:col>
<:col :let={{_id, beacon}} label="EIRP (mW)">{Beacon.format_mw(beacon.power_mw)}</:col>
<:col :let={{_id, beacon}} label="Height AGL (ft)">{beacon.height_ft}</:col>
<:col :let={{_id, beacon}} label="Keying">{Beacon.keying_label(beacon.keying)}</:col>
@ -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

View file

@ -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