Mazmuna geçiň

Module:Math/tonumber

Wikipediýa, erkin ensiklopediýa

Bu modul üçin resminamalar Module:Math/tonumber/doc sahypasynda döredilip bilner

--[[

This module convert strings to numbers.

]]
local p = {}

-- Get first integer number from string.
function p.integer( frame )
    local s = frame.args[1]

	s = string.gsub( s, '<[^<>]+>', '' ) -- strip HTML tags
    s = string.gsub( s, '[^0-9]', ' ' )
    s = mw.text.trim( s )
    s = mw.text.split( s, ' ' )[1]
    
    return tonumber( s )
end

-- Get number from Wikidata quantity.
function p.quantity( frame )
	local s = frame.args[1]
    s = string.gsub( s, ' ', '' )
    s = string.gsub( s, '±.*$', '' )

    return tonumber( s )
end

-- Get year value from string.
function p.year( frame )
    local n = nil
    local cat = frame.args['cat']

	-- 'ýylda' or 'ýylyň'
    local yearForm = 'ýylda'
    if frame.args['form'] and frame.args['form'] ~= '' then
    	yearForm = frame.args['form']
	end

    local s = frame.args[1]
	s = string.gsub( s, '<[^<>]+>', '' ) -- strip HTML tags

    local isBce = string.match( s, 'b%se\.%s?ö' )
    s = string.gsub( s, 'ýyl%sb%se\.%s*ö\.?', '' )
    s = string.gsub( s, 'b%sö\.%s*ö\.?', '' )

	-- Get first 3- or 4-digit integer number from string.
    local sParts = string.gsub( s, '[^0-9]', ' ' )
    sParts = mw.text.trim( sParts )
    sParts = mw.text.split( sParts, ' +' )
    for k, v in pairs( sParts ) do
        if string.match( v, '^[12]?%d%d%d$' ) then
            n = tonumber( v )
            break
        end
    end

	-- The entire string is a number.
	if not n then
	    s = string.gsub( s, '[%[%]]', '' )
	    s = mw.text.trim( s )
		if string.match( s, '^%d%d?%d?%d?$' ) then
			n = tonumber( s )
		end
	end

	-- Generate category
	if n then
	    if isBce then
	    	if cat then
	    		return '[[К:' .. cat .. ' ' .. n .. ' ' .. yearForm .. ' b.e.ö.]]'
	    	end
	    	n = -n
	    else
	    	if cat then 
	    		return '[[К:' .. cat .. ' ' .. n .. ' ' .. yearForm .. ']]'
	    	end
	    end
	end

    return n or frame.args['default']
end

return p