Anonymous

Module:Number: Difference between revisions

From Melvor Idle
Force removal of scientific notation on formatnum
m (Fix again...)
(Force removal of scientific notation on formatnum)
Line 40: Line 40:
return _x
return _x
end   
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.
Line 46: Line 47:
if tonumber(number) == nil then
if tonumber(number) == nil then
return number
return number
else
end
local result = number
while true do
-- Find out of the number is using scientific notation.
-- Format in blocks of 3 digits at a time until formatting is complete
-- If it is, convert it to a string and remove the trailing zeroes.
            local k
local result = tostring(number)
result, k = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2')
if result:find("[eE]") ~= nil then
if k == 0 then
result = string.format("%.20f", number)
break
result = result:gsub("%.?0*$", "")
end
end
 
while true do
-- Format in blocks of 3 digits at a time until formatting is complete
        local k
result, k = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2')
if k == 0 then
break
end
end
return result
end
end
return result
end     
end     


2,875

edits