Modulo:Wikidata2/Formatters/approx

Dokumentado por ĉi tiu modulo povas esti kreata ĉe Modulo:Wikidata2/Formatters/approx/dokumentado

require('strict')

local p = {}

local parent = require 'Modulo:Wikidata2/Formatters/quantity'

local inflection = {
	THOUSANDS = {
		['1'] = 'tisíc',
		['2'] = 'tisíc',
		['3'] = 'tisícům',
		['4'] = 'tisíc',
		['6'] = 'tisících',
		['7'] = 'tisíci',
	},
	MILLIONS = {
		['1'] = 'milionu',
		['2'] = 'milionu',
		['3'] = 'milionu',
		['4'] = 'milionu',
		['6'] = 'milionu',
		['7'] = 'milionu',
	},
}

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(map, case)
	return map[case or '1']
end

function p.formatNumber(value, options)
	local formatNumber = parent.formatNumber
	if 1e3 < value and value < 1e4 then
		return mw.ustring.format('ĉirkaŭ %s', formatNumber(roundTo(value, 100), options))
	elseif 1e4 <= value and value < 1e6 then
		return mw.ustring.format('ĉirkaŭ %s %s',
			formatNumber(roundTo(value, 1000) / 1000, options),
			doInflection(inflection.THOUSANDS, options.case))
	elseif 1e6 < value and value < 1e8 then
		return mw.ustring.format('ĉirkaŭ %s %s',
			formatNumber(roundTo(value, 1000) / 1e6, options),
			doInflection(inflection.MILLIONS, 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