Modulo:Conversione

Info Istruzioni per l'uso
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Conversione/man (modifica · cronologia)
Sandbox: Modulo:Conversione/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Conversione/test (modifica · cronologia · esegui)

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('strict')

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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.