Moduuli:Cs:Wikidata/Formatters/quantity
Ulkoasu
Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Cs:Wikidata/Formatters/quantity/ohje
require 'Module:No globals'
local p = {}
local lib = require 'Module:cs:Wikidata/lib'
local function formatUnit(entityId)
local Formatters = require 'Module:cs:Wikidata/Formatters'
return Formatters.formatRawValue(entityId, 'wikibase-entityid', { label = 'unitsymbol' })
end
local function formatNumber(number)
local integer, fractional
local prefix = ''
if number < 0 then
number = -number
prefix = '−'
end
number = tostring(number)
if mw.ustring.find(number, '%.') then
integer, fractional = mw.ustring.match(number, '^(.+)%.(.+)$')
else
integer = number
end
local length = mw.ustring.len(integer)
local i = length % 3
if i == 0 then
i = 3
end
local formatted_num = mw.ustring.sub(integer, 1, i)
while i < length do
formatted_num = formatted_num .. ' ' .. mw.ustring.sub(integer, i + 1, i + 3)
i = i + 3
end
if fractional then
local length = mw.ustring.len(fractional)
local i = 3
formatted_num = formatted_num .. ',' .. mw.ustring.sub(fractional, 1, 3)
while i < length do
formatted_num = formatted_num .. ' ' .. mw.ustring.sub(fractional, i + 1, i + 3)
i = i + 3
end
end
return prefix .. formatted_num
end
local function getUnitAndCoef(unit, options)
local entityId = lib.getItemIdFromURI(unit)
if not entityId or entityId == 'Q199' then
return nil, 1
end
local coef
if options.unit and entityId ~= options.unit then
local WD = require 'Module:cs:Wikidata'
coef = WD.getRawValueFromLua{
id = entityId,
property = 'P2370',
withunit = options.unit,
limit = 1
} or WD.getRawValueFromLua{
id = entityId,
property = 'P2442',
withunit = options.unit,
limit = 1
}
if coef then
entityId = options.unit
end
end
return entityId, coef or 1
end
function p.getRawValue(value, options)
local _, coef = getUnitAndCoef(value.unit, options)
return tonumber(value.amount) * coef
end
function p.formatRawValue(value, options)
return formatNumber(value)
end
function p.formatValue(value, options)
local unit, coef = getUnitAndCoef(value.unit, options)
local amount = tonumber(value.amount) * coef
local margin
if lib.IsOptionTrue(options, 'showmargin') then
if value.upperBound then
margin = tonumber(value.upperBound) * coef - amount
end
end
amount = formatNumber(amount)
if margin then
amount = amount .. '±' .. formatNumber(margin)
end
if tostring(options.showunit) ~= 'false' then
if unit then
return amount .. ' ' .. formatUnit(unit)
end
end
return amount
end
return p