Old School RuneScape Wiki
(I haven't worked much with modules before, so I'm not sure if this is the preferred way to do it. I think it should at least work though.)
m (12 revisions)
 
(3 intermediate revisions by one other user not shown)
Line 22: Line 22:
   
 
function p.round( frame )
 
function p.round( frame )
  +
return p._round( frame.args[1], frame.args[2] )
-- @todo implement for use as a template or through #invoke
 
 
end
 
end
   
Line 35: Line 35:
   
 
function p._short( str )
 
function p._short( str )
local num = string.sub( str, 1, -2 )
+
local num = str:sub( 1, -2 )
local char = string.sub( str, -1 )
+
local char = str:sub( -1 )
if toNumber( num ) then
+
if tonumber( num ) then
 
if char == 'k' then
 
if char == 'k' then
 
num = num * 10^3
 
num = num * 10^3

Latest revision as of 00:55, 2 September 2015

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

--
-- Number manipulation methods
--
-- Many methods are already available in the math library, so this will be fairly small
--

local p = {}

--
-- Rounds a number to a specified number of decimal places
--
-- If dp is not specified, it defaults to zero
-- based on <http://dev.wikia.com/wiki/Module:HF>
--
function p._round( num, dp )
    if not num then
        return
    end
    local mult = 10 ^ ( dp or 0 )
    return math.floor( num * mult + 0.5 ) / mult
end

function p.round( frame )
    return p._round( frame.args[1], frame.args[2] )
end

--
-- Expands a shorthand number to an actual number
--
-- @example 3.5k -> 3500
-- @example 10m -> 10000000
--
-- Returns the string if it is not a valid number
--

function p._short( str )
    local num = str:sub( 1, -2 )
    local char = str:sub( -1 )
    if tonumber( num ) then
        if char == 'k' then
            num = num * 10^3
        elseif char == 'm' then
            num = num * 10^6
        elseif char == 'b' then
            -- strip trailing letter
            num = num * 10^9
        else
            return str
        end
        return num
    else
        return str
    end
end

function p.short( frame )
    return p._short( frame.args[1] )
end

return p