Jump to content

Editing Module:NumberAbbr

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
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),11) 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 ps=f.args[5]==nil and false or f.args[5] --add plus symbol to positive numbers?
local chk=string.gsub(n,",","") --remove commas
local n2="" --create additional variables
if tonumber(chk)==nil then --cancel abbreviation if number is invalid
local nn=""
return n
local ep=0
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
n=string.sub(n,m+1,#n) --if so, remove minus at start
if string.sub(n,1,1)=="i" then --check if infinite
nn=math.huge
isinf=true
else
else
local n2="" --create additional variables
for nc in string.gmatch(n,"[1234567890e%.%-%+]+") do --extract valid numerical characters
local nn=""
nn=nn..nc
local ep=0
end
local isinf=false
n2=nn --keep full number for later
local nb=n --keep unmodified number if needed
local m=string.sub(n,1,1)=="-" and 1 or 0 --check if negative
ep=string.find(nn,"e")~=nil and tonumber(string.sub(nn,string.find(nn,"e")+1,#nn)) or #nn>100 and #nn or math.floor(math.log10(nn)) --get exponent
n=string.sub(n,m+1,#n) --if so, remove minus at start
if string.find(n,"i")~=nil then --check if infinite
if string.sub(ep,1,1)=="i" then --check if exponent is infinite
nn=math.huge
nn=math.huge
isinf=true
isinf=true
else
else
for nc in string.gmatch(n,"[0-9e%.%-%+]+") do --extract valid numerical characters
nn=string.find(nn,"e")~=nil and string.sub(nn,1,string.find(nn,"e")-1) or nn --remove the exponent from number variable
nn=nn..nc
end
n2=nn --keep full number for later
ep=string.find(nn,"e")~=nil and tonumber(string.sub(nn,string.find(nn,"e")+1,#nn)) or #nn>100 and #nn or math.floor(math.log10(nn)) --get exponent
if tonumber(nn)>999 then --pad variable to 14 characters
if #nn>14 then
if string.find(ep,"i")~=nil then --check if exponent is infinite
nn=string.sub(nn,1,14)
nn=math.huge
else
isinf=true
for i=#nn,13 do
else
nn=nn.."0"
nn=string.find(nn,"e")~=nil and string.sub(nn,1,string.find(nn,"e")-1) or nn --remove the exponent from number variable
local nd=nn
nn=""
for nc in string.gmatch(nd,"[0-9]+") do --remove decimal point
nn=nn..nc
end
if ep>=3 then --pad variable to 14 characters
if #nn>14 then
nn=string.sub(nn,1,14)
else
for i=#nn,13 do
nn=nn.."0"
end
end
end
end
end
nn=tonumber(nn)>999 and tonumber(nn)/10^(13-ep%3) or nn
nn=math.floor(nn*10^dec)/10^dec --remove unnecessary decimal places
end
end
nn=tonumber(nn)>999 and tonumber(nn)/10^(13-ep%3) or nn
nn=math.floor(nn*10^dec)/10^dec --remove unnecessary decimal places
end
end
m=m>0 and "-" or chk~="0" and ps and "+" or ""
return ep>=s and (abt and "<abbr title=\""..m..n.."\">" or "")..m..nn..""..(not isinf and p.illion({args={math.floor(ep/3-1)}}) or "")..(abt and "</abbr>" or "") or m..n
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 nb
end
end


Line 79: Line 66:
if n>=math.huge then
if n>=math.huge then
return math.huge
return math.huge
elseif n>999 then
elseif n>=999 then
i=10^(3*math.floor(math.log10(n)/3))
i=10^(3*math.floor(math.log10(n)/3))
return (math.floor(n/i)>1 and p.illion({args={math.floor(n/i),false}}) or "")..(math.floor(math.log10(n)/3)<=#e3 and e3[math.floor(math.log10(n)/3)] or "["..math.floor(math.log10(n)/3).."]")..(r<3 and p.illion({args={n%i,nil,true,r+1}}) or "")..(r==3 and "..." or "") --it can only go so far down, this limit is to prevent not enough memory errors
return (math.floor(n/i)>1 and p.illion({args={math.floor(n/i),false}}) or "")..(math.floor(math.log10(n)/3)<=#e3 and e3[math.floor(math.log10(n)/3)] or "["..math.floor(math.log10(n)/3).."]")..(r<10 and p.illion({args={n%i,nil,true,r+1}}) or "")..(r==10 and "..." or "") --it can only go so far down, this limit is to prevent not enough memory errors
else
else
return n<b0 and "" or (u00 and n<#e00) and e00[1+n] or e0[1+(n%10)]..e1[1+(math.floor(n/10)%10)]..e2[1+(math.floor(n/100)%10)]
return n<b0 and "" or (u00 and n<#e00) and e00[1+n] or e0[1+(n%10)]..e1[1+(math.floor(n/10)%10)]..e2[1+(math.floor(n/100)%10)]
Please note that all contributions to Keynotathlon Wiki are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) (see Keynotathlon Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)

Template used on this page:

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