Jump to content

Module:NumberAbbr: Difference between revisions

MisterChuChu (talk | contribs)
No edit summary
MisterChuChu (talk | contribs)
Blank input fix
Line 2: Line 2:
function p.abbr(frame)
function p.abbr(frame)
local f=frame
local f=frame
local n=f.args[1]~=nil and tostring(f.args[1]) or 0 --number to abbreviate
local n=(f.args[1]~=nil and string.match(f.args[1],"[1234567890e%.%-%+]+")~=nil) and tostring(f.args[1]) or "0" --number to abbreviate
local dec=f.args[2]~=nil and math.min(math.max(f.args[2],0),9) or 3 --decimal places
local dec=f.args[2]~=nil and math.min(math.max(f.args[2],0),9) or 3 --decimal places
local s=f.args[3]~=nil and math.max(f.args[3],3) or 6 --start at 10^
local s=f.args[3]~=nil and math.max(f.args[3],3) or 6 --start at 10^
local abt=f.args[4]==nil and false or f.args[4] --use abbr tag?
local abt=f.args[4]==nil and false or f.args[4] --use abbr tag?
local n2=""
local n2="" --create additional variables
local nn="" --create additional variables
local nn=""
local ep=0
local ep=0
local isinf=false
local isinf=false
local nb=n --keep unmodified number if needed
local m=string.sub(n,1,1)=="-" and 1 or 0 --check if negative
local m=string.sub(n,1,1)=="-" and 1 or 0 --check if negative
Line 47: Line 48:
end
end
return (math.log10(n2)>=3 and ep>=s) and (abt and "<abbr title=\""..n.."\">" or "")..(m>0 and "-" or "")..nn..""..(not isinf and p.illion({args={math.floor(ep/3-1)}}) or "")..(abt and "</abbr>" or "") or f.args[1]
return (math.log10(n2)>=3 and ep>=s) and (abt and "<abbr title=\""..n.."\">" or "")..(m>0 and "-" or "")..nn..""..(not isinf and p.illion({args={math.floor(ep/3-1)}}) or "")..(abt and "</abbr>" or "") or nb
end
end


Cookies help us deliver our services. By using our services, you agree to our use of cookies.