Module:WikidataIB/sandbox1: Difference between revisions

Content deleted Content added
decimalToDMS function to reduce duplicated code
decimalPrecision function to match decimal output to precision given
Line 133:
end
return d, m, s
end
 
 
-------------------------------------------------------------------------------
-- decimalPrecision takes a decimal (x) with precision (p)
-- and returns x rounded approximately to the given precision
-- precision should be between 1 and 1e-6, preferably a power of 10.
local function decimalPrecision(x, p)
if p > 1 then p = 1 end
if p < 1e-6 then p = 1e-6 end
local e = math.floor(math.log10(p))
local m = math.floor(x / 10^e + 0.5)
x = m * 10^e
-- if it's integral, cast to an integer:
if x == math.floor(x) then x = math.floor(x) end
return x
end
 
Line 602 ⟶ 618:
end
if form == "dec" then
lat = decimalPrecision(lat, prec)
long = decimalPrecision(long, prec)
out[#out+1] = lat .. "°" .. ns .. " " .. long .. "°" .. ew
else