WoW:USERAPI round

From AddOn Studio
Revision as of 10:52, 16 December 2008 by WoWWiki>Rolandius (bolded title of article)
Jump to navigation Jump to search
This page documents a <i>user-defined function</i> that you can copy and paste into your addon. Replace PREFIX with your addon or lib prefix to avoid conflicts between different versions of these functions.

User defined functions


Round a number off to n places.

number = round(input, n)

Function Parameters

Arguments

input
Number. The number to round
n
Number. Round to this many places. If nil, round to 0 places.

Returns

number
Number. The rounded number.

Example

local number = round(math.pi, 2)
print(number) -- prints 3.14

Code

function round(input, places)
    if not places then
        places = 0
    end
    if type(input) == "number" and type(places) == "number" then
        local pow = 1
        for i = 1, ceil(places) do
            pow = pow * 10
        end
        return floor(input * pow + 0.5) / pow
    end
end