Modulo:Conversione/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
aggiornato alla versione corrente
Omega Bot (discussione | contributi)
m Bot: rimuovo no globals obsoleto
 
(2 versioni intermedie di un altro utente non mostrate)
Riga 6:
]]--
 
require('Modulo:No globalsstrict')
 
local cfg = mw.loadData('Modulo:Conversione/Configurazione/sandbox')
local p = {}
 
Riga 19:
 
local function getUnit(val, targetunitdata, args)
local ret, link, space
 
if args.showunit == 'long' then
ret = targetunitdata.symbol
elseif args.showunit == 'true'showunitlong then
-- unità per esteso
ret = val > 1 and targetunitdata.name2 or targetunitdata.name1
elseif args.showunit == 'true' then
ret = targetunitdata.symbol
end
 
if args.showlink == 'true'showunitlink then
link = targetunitdata.link
elseif type(args.showlink) == 'string' then
link = args.showlink
end
 
returnspace ' '= (args.. (linkshowunit and '[[' .targetunitdata.nospace) link ..and '|' ..or ret ..' ']]' or ret)
 
return space .. (link and '[[' .. link .. '|' .. ret .. ']]' or ret)
end
 
-- Ritorna il valore convertito alla unità di misura e alle opzioni specificate
function p._main(valstrval, sourceunit, targetunit, args)
local sourceunitdataval, targetunitdatasourceunitdata, unittargetunitdata, cat
 
val = tonumber(valstrval)
-- se non è un numero ritorna la stringa non modificata
if not val then
return valstrval
end
if not args then
Riga 59:
error('unità di misura incompatibili: ' .. sourceunitdata.type .. ' e ' .. targetunitdata.type)
end
val = val *if sourceunitdata.scale /== targetunitdata.scale then
-- nothing
elseif sourceunitdata.type == 'temperature' then
val = (val - sourceunitdata.offset) * sourceunitdata.scale
val = val / targetunitdata.scale + targetunitdata.offset
else
val = val * sourceunitdata.scale / targetunitdata.scale
end
else
cat = '[[Categoria:Pagine con unità di misura non supportata]]'
end
 
-- eventuale arrotondamento
if args.rounding then
val = round(val, args.rounding)
end
 
-- formatnum
if targetunitdata and (args.showunit == 'true' or args.showunit == 'long') then
if args.formatnum then
unit = getUnit(val, targetunitdata, args)
val = mw.language.getContentLanguage():formatNum(val)
end
 
-- unità di misura
if targetunitdata and (args.showunit == 'true' or args.showunit == 'long'showunitlong) then
unitval = val .. getUnit(val, targetunitdata, args)
end
 
return val .. (unit or '') .. (cat or '')
end