Module:HELLO

From Wikipedia

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

HELLO = {}
local precision = {}

--- Simple round function
function precision.round(num, idp)
    local mult = 10^(idp or 0)
    if num >= 0 then return math.floor(num * mult + 0.5) / mult
    else return math.ceil(num * mult - 0.5) / mult end
end

--- Number of trailing zero's in the string num
function tz(num)
    local trailing = 0
    for i=num:len(), 1,-1 do
        if num:sub(i,i) == "0" then
            trailing = trailing + 1
        else
            break
        end
    end

    return trailing
end

--- Find the precision of a number
-- TODO perhaps make this count chars after . ?
-- @param num the number of which you want to determine the level of precision
function one(input)
    if input == precision.round( input, 0) then
        return 0
    elseif input == precision.round( input, 1) then
        return 1
    elseif input == precision.round( input, 2) then
        return 2
    elseif input == precision.round( input, 3) then
        return 3
    elseif input == precision.round( input, 4) then
        return 4
    elseif input == precision.round( input, 5) then
        return 5
    else
        return 6
    end
end

--- Get the precision of a numeric value
-- 
function precision.prec1(num)
    local input = assert( tonumber(num), "Input for precision not a numeric value" );
    
   if input == 0 then
        return 0
    else
        -- tz takes the string input of the number
        return one(input) + tz(num)
    end
end

--- Get the precision of a numeric value
function precision.one(frame)
    prec1(frame.args[1])
end

return "HELLO"