Přeskočit na obsah

Modul:Wikidata/Formatters/approx

Tuto stránku mohou editovat jen zavedení uživatelé a správci.
Z Wikipedie, otevřené encyklopedie

require 'Modul:No globals'

local p = {}

local parent = require 'Modul:Wikidata/Formatters/quantity'

p.getRawValue = parent.getRawValue

local function roundTo(value, factor)
	local remainder = value % factor
	if remainder < (factor / 2) then
		return math.floor(value / factor) * factor
	else
		return math.ceil(value / factor) * factor
	end
end

local function doInflection(case)
	return ({
		['1'] = 'tisíc',
		['2'] = 'tisíc',
		['3'] = 'tisícům',
		['4'] = 'tisíc',
		['6'] = 'tisících',
		['7'] = 'tisíci',
	})[case or '1']
end

function p.formatNumber(value, options)
	local formatNumber = parent.formatNumber
	if 1000 < value and value < 10000 then
		return mw.ustring.format('přibližně %s', formatNumber(roundTo(value, 100), options))
	elseif 10000 < value and value < 1000000 then
		return mw.ustring.format('přibližně %s %s',
			formatNumber(roundTo(value, 1000) / 1000, options), doInflection(options.case))
	else
		return formatNumber(value, options)
	end
end

p.formatRawValue = p.formatNumber

function p.formatValue(value, options)
	parent.setFormatNumber(p.formatNumber)
	return parent._formatValue(value, options)
end

return p