Module:Number

From Old School Near-Reality Wiki
Revision as of 13:06, 9 November 2022 by Jacmob (talk | contribs) (Created page with "--[[ {{Helper module |name = Number |fname1 = _round(num, dp) |ftype1 = float, int |fuse1 = Rounds <code>num</code> to a precision given by <code>dp</code>, if <code>dp</code> is not specified, it defaults to zero. |fname2 = _short(str) |ftype2 = String |fuse2 = Convert numbers ending in k, m or b to the actual correct number. Example: 3.5k -> 3500 }} -- ]] -- -- Number manipulation methods -- -- Many methods are already available in the math library, so this will be fai...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Number/doc. [edit] [history] [purge]
Module:Number is required by Module:ChangePerDay.
Module:Number is required by Module:ExchangeDefault.

This module is a helper module to be used by other modules; it may not be designed to be invoked directly. See Near-Reality:Lua/Helper modules for a full list and more information.

ModuleFunctionTypeUse
Number_round(num, dp)float, intRounds num to a precision given by dp, if dp is not specified, it defaults to zero. Rounds half up.
_short(str)StringConvert numbers ending in k, m or b to the actual correct number. Example: 3.5k -> 3500

--[[
{{Helper module
|name = Number
|fname1 = _round(num, dp)
|ftype1 = float, int
|fuse1 = Rounds <code>num</code> to a precision given by <code>dp</code>, if <code>dp</code> is not specified, it defaults to zero.
|fname2 = _short(str)
|ftype2 = String
|fuse2 = Convert numbers ending in k, m or b to the actual correct number. Example: 3.5k -> 3500
}}
-- ]]
--
-- 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