Modul:SK/Helper: razlika između inačica
< Modul:SK
mNema sažetka uređivanja |
mNema sažetka uređivanja |
||
| Nisu prikazane 3 međuinačice | |||
| Redak 1: | Redak 1: | ||
local h = {} | local h = {} | ||
-- | -- Funkcija koja popravlja sve: razmake, tablice i pretvara u broj ako treba | ||
local function | local function fix(val, asNumber) | ||
if type(val) == 'table' then val = val[1] end -- | if type(val) == 'table' then val = val[1] end | ||
return tonumber(val) | if type(val) == 'string' then | ||
val = val:match("^%s*(.-)%s*$") -- Briše razmake (Trim) | |||
end | |||
if asNumber then | |||
return tonumber(val) | |||
end | |||
return val | |||
end | end | ||
-- Format broja s tisućicama | -- Format broja s tisućicama | ||
function h.num(n) | function h.num(n) | ||
local num = | local num = fix(n, true) | ||
if not num then return "" end | if not num then return "" end | ||
local s = tostring(math.floor(num)) | local s = tostring(math.floor(num)) | ||
local i, j, minus, int, fraction = s:find('^([-+]?)(%d+)([.]?%d*)$') | local i, j, minus, int, fraction = s:find('^([-+]?)(%d+)([.]?%d*)$') | ||
| Redak 20: | Redak 25: | ||
-- Format površine | -- Format površine | ||
function h.area(a) | function h.area(a) | ||
local num = | local num = fix(a, true) | ||
if not num then return "" end | if not num then return "" end | ||
return string.format("%.2f", num):gsub("%.", ",") .. " km²" | return string.format("%.2f", num):gsub("%.", ",") .. " km²" | ||
| Redak 27: | Redak 32: | ||
-- Format gustoće | -- Format gustoće | ||
function h.density(d) | function h.density(d) | ||
local num = | local num = fix(d, true) | ||
if not num then return "" end | if not num then return "" end | ||
return string.format("%.1f", num):gsub("%.", ",") .. " st./km²" | return string.format("%.1f", num):gsub("%.", ",") .. " st./km²" | ||
end | end | ||
-- | -- Za naziv (čisti tekst) | ||
function h. | function h.text(t) | ||
return | return fix(t, false) or "" | ||
end | end | ||
function h.pop(p) return h.num(p) end | |||
return h | return h | ||
Posljednja izmjena od 26. ožujak 2026. u 12:43
Script error: The function "nonexistent" does not exist.
local h = {}
-- Funkcija koja popravlja sve: razmake, tablice i pretvara u broj ako treba
local function fix(val, asNumber)
if type(val) == 'table' then val = val[1] end
if type(val) == 'string' then
val = val:match("^%s*(.-)%s*$") -- Briše razmake (Trim)
end
if asNumber then
return tonumber(val)
end
return val
end
-- Format broja s tisućicama
function h.num(n)
local num = fix(n, true)
if not num then return "" end
local s = tostring(math.floor(num))
local i, j, minus, int, fraction = s:find('^([-+]?)(%d+)([.]?%d*)$')
int = int:reverse():gsub('(%d%d%d)', '%1 '):reverse():gsub('^ ', '')
return minus .. int .. fraction
end
-- Format površine
function h.area(a)
local num = fix(a, true)
if not num then return "" end
return string.format("%.2f", num):gsub("%.", ",") .. " km²"
end
-- Format gustoće
function h.density(d)
local num = fix(d, true)
if not num then return "" end
return string.format("%.1f", num):gsub("%.", ",") .. " st./km²"
end
-- Za naziv (čisti tekst)
function h.text(t)
return fix(t, false) or ""
end
function h.pop(p) return h.num(p) end
return h