Modulo:Conversione: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Copio da https://it.wikipedia.org/w/index.php?title=Modulo:Conversione&oldid=76074367
(Nessuna differenza)

Versione delle 11:06, 31 mar 2020

Modulo per convertire un valore numerico da una unità di misura a un'altra.

Ha una sottopagina di configurazione: Modulo:Conversione/Configurazione.


--[[
* Modulo per effettuare la conversione di unità di misura.
*
* Ampiamente modificato a partire da:
* http://fr.wikipedia.org/w/index.php?title=Module:Conversion&oldid=118515752
]]--

require('Modulo:No globals')

local cfg = mw.loadData('Modulo:Conversione/Configurazione')
local p = {}

-- Ritorna il numero arrotondato al numero di cifre decimali richiesto
-- http://lua-users.org/wiki/SimpleRound
local function round(num, idp)
	local mult = 10 ^ (idp or 0)
	return math.floor(num * mult + 0.5) / mult
end

local function getUnit(val, targetunitdata, args)
	local ret, link, space

	if args.showunit then
		ret = targetunitdata.symbol
	elseif args.showunitlong then
	 	-- unità per esteso
		ret = val > 1 and targetunitdata.name2 or targetunitdata.name1
	end

	if args.showunitlink then
		link = targetunitdata.link
	end

	space = (args.showunit and targetunitdata.nospace) and '' or ' '

	return space .. (link and '[[' .. link .. '|' .. ret .. ']]' or ret)
end

-- Ritorna il valore convertito alla unità di misura e alle opzioni specificate
function p._main(strval, sourceunit, targetunit, args)
	local val, sourceunitdata, targetunitdata, cat

	val = tonumber(strval)
	-- se non è un numero ritorna la stringa non modificata
	if not val then
		return strval
	end
	if not args then
		args = {}
	end

	if sourceunit and not targetunit then
		targetunit = sourceunit
	end
	sourceunitdata = cfg.units[sourceunit] or cfg.units[cfg.alias[sourceunit]]
	targetunitdata = cfg.units[targetunit] or cfg.units[cfg.alias[targetunit]]
	if sourceunitdata and targetunitdata then
		if sourceunitdata.type ~= targetunitdata.type then
			error('unità di misura incompatibili: ' .. sourceunitdata.type .. ' e ' .. targetunitdata.type)
		end
		if sourceunitdata == targetunitdata 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

	-- arrotondamento
	if args.rounding then
		val = round(val, args.rounding)
	end

	-- formatnum
	if args.formatnum then
		val = mw.language.getContentLanguage():formatNum(val)
	end

	-- unità di misura
	if targetunitdata and (args.showunit or args.showunitlong) then
		val = val .. getUnit(val, targetunitdata, args)
	end

	return val .. (cat or '')
end

-- Entry-point per {{#invoke:Conversione|main|...}}
function p.main(frame)
	local args = frame.args
	return p._main(args[1], args[2], args[3], args)
end

return p