« Module:WeightToPercent » : différence entre les versions
Aller à la navigation
Aller à la recherche
tentative d'arrondi 1 décimale |
Aucun résumé des modifications |
||
| Ligne 2 : | Ligne 2 : | ||
function p.convert(frame) | function p.convert(frame) | ||
local weight = frame.args | -- frame.args values are strings in MediaWiki, so force numeric conversion | ||
local totalweight = frame.args | local weight = tonumber(frame.args.weight) | ||
local totalweight = tonumber(frame.args.total) | |||
if weight == nil then return "ERROR: weight input ( | |||
elseif totalweight == nil then return "ERROR: totalweight ( | if weight == nil then | ||
elseif weight == 0 then return 0 | return "ERROR: weight input (arg 'weight') is nil or not a number" | ||
elseif totalweight == nil then | |||
return "ERROR: totalweight input (arg 'total') is nil or not a number" | |||
elseif totalweight == 0 then | |||
return "ERROR: totalweight (arg 'total') is 0" | |||
elseif weight == 0 then | |||
return 0 | |||
end | |||
return tonumber(string.format("%.1f", (weight * 100) / totalweight)) | |||
end | end | ||
return p | return p | ||
Version du 2 mars 2026 à 18:05
La documentation pour ce module peut être créée à Module:WeightToPercent/doc
local p = {}
function p.convert(frame)
-- frame.args values are strings in MediaWiki, so force numeric conversion
local weight = tonumber(frame.args.weight)
local totalweight = tonumber(frame.args.total)
if weight == nil then
return "ERROR: weight input (arg 'weight') is nil or not a number"
elseif totalweight == nil then
return "ERROR: totalweight input (arg 'total') is nil or not a number"
elseif totalweight == 0 then
return "ERROR: totalweight (arg 'total') is 0"
elseif weight == 0 then
return 0
end
return tonumber(string.format("%.1f", (weight * 100) / totalweight))
end
return p