Anonymous

Module:Number: Difference between revisions

From Melvor Idle
Add magical autoround function
m (Fix parameter naming)
(Add magical autoround function)
Line 16: Line 16:
         d = 1e30
         d = 1e30
     }
     }
   
 
local function sigfig(x, p)
local x_sign = x < 0 and -1 or 1
local x = math.abs(x)
local n = math.floor(math.log10(x)) + 1 - p
return x_sign * math.pow(10, n) * p.round2(x / math.pow(10, n), 0)
end
 
-- Automatically rounds to 2 places from the significant figure.
-- Taken from RSWiki.
function p.autoround(x)
x = tonumber(x) or 0
local _x
if x == 0 then
_x = 0
elseif math.abs(x) < 0.1 then
_x = sigfig(x, 2)
elseif math.abs(x) > 999 then
_x = p.round2(x, 0)
else
_x = p.round2(x, 2)
end
return _x
end 
--- Formats a number by inserting commas as thousand separators.
--- Formats a number by inserting commas as thousand separators.
-- @param number (number or string) The number to format.
-- @param number (number or string) The number to format.
2,875

edits