Module:Math/tonumber

Wikipedia дан
Jump to navigation Jump to search

Documentation for this module may be created at Module:Math/tonumber/doc

--[[

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, '[^0-9]', ' ' )
    s = mw.text.trim( s )
    s = mw.text.split( s, ' ' )[1]
    
    return tonumber(s)
end

-- Get first 4-digit integer number from string.
function p.year( frame )
    local s = frame.args[1]
    
    s = string.gsub( s, '[^0-9]', ' ' )
    s = mw.text.trim( s )
    s = mw.text.split( s, ' +' )

    n = frame.args['default'] or nil
    for k, v in pairs( s ) do
        if string.match( v, '^[1-9]%d%d%d$' ) then
            n = tonumber(v)
            break
        end
    end

    return n
end

return p