WoW:USERAPI round: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (New page: {{userfunc}} <!-- Leave this line in! --> Round a number off to n places. number = round(input, n) == Function Parameters == === Arguments === ;input : Number. The number to round ;n : ...)
 
m (bolded title of article)
Line 1: Line 1:
{{userfunc}} <!-- Leave this line in! -->
{{userfunc}} <!-- Leave this line in! -->


Round a number off to n places.
 
'''Round''' a number off to n places.
  number = round(input, n)
  number = round(input, n)



Revision as of 10:52, 16 December 2008

This page documents a user-defined function 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