Module:FloorNumber: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
No edit summary Tag: Reverted |
No edit summary Tag: Manual revert |
||
Line 1: | Line 1: | ||
local ordinal = require('Module:Ordinal')._main |
|||
--[[ |
|||
local yn = require('Module:Yesno') |
|||
{{Helper module |
|||
|name = Number |
|||
|fname1 = _round(num, dp) |
|||
|ftype1 = float, int |
|||
|fuse1 = Rounds <code>num</code> to a precision given by <code>dp</code>, if <code>dp</code> is not specified, it defaults to zero. |
|||
|fname2 = _short(str) |
|||
|ftype2 = String |
|||
|fuse2 = Convert numbers ending in k, m or b to the actual correct number. Example: 3.5k -> 3500 |
|||
}} |
|||
-- ]] |
|||
-- |
|||
-- Number manipulation methods |
|||
-- |
|||
-- Many methods are already available in the math library, so this will be fairly small |
|||
-- |
|||
local p = {} |
local p = {} |
||
function p.main(frame) |
|||
-- |
|||
return p._main(frame:getParent().args) |
|||
-- Rounds a number to a specified number of decimal places |
|||
-- |
|||
-- If dp is not specified, it defaults to zero |
|||
-- based on <http://dev.wikia.com/wiki/Module:HF> |
|||
-- |
|||
function p._round( num, dp ) |
|||
if not num then |
|||
return |
|||
end |
|||
local mult = 10 ^ ( dp or 0 ) |
|||
return math.floor( num * mult + 0.5 ) / mult |
|||
end |
end |
||
function p. |
function p._main(args) |
||
local nohelp = yn(args.nohelp) |
|||
return p._round( frame.args[1], frame.args[2] ) |
|||
local caps = yn(args.caps) |
|||
end |
|||
local flr = tonumber(args.uk) |
|||
-- prevent people from using anything but the main syntax for consistency across the wiki. Big red errors should make stuff clear. |
|||
-- |
|||
assert(flr, "Missing parameter `uk`") |
|||
-- Expands a shorthand number to an actual number |
|||
-- prevent unnamed parameters, which indicates people wanting to use deprecated |nohelp}}, |caps}}, or {{floornumber|1}} syntax. |
|||
-- |
|||
assert(args[1] == nil, "Unrecognised parameter") |
|||
-- @example 3.5k -> 3500 |
|||
local flr_us = flr+1 |
|||
-- @example 10m -> 10000000 |
|||
local ord_gb = ordinal(flr, {nosup=true, nonum=true}) |
|||
-- |
|||
local ord_us = ordinal(flr_us, {nosup=true, nonum=true}) |
|||
-- Returns the string if it is not a valid number |
|||
-- |
|||
local ret = mw.html.create('span') |
|||
function p._short( str ) |
|||
ret:addClass('floornumber') |
|||
local num = str:sub( 1, -2 ) |
|||
local |
local spn_gb = ret:tag('span') |
||
spn_gb :addClass('floornumber-gb') |
|||
if tonumber( num ) then |
|||
if char == 'k' then |
|||
if flr == 0 then |
|||
num = num * 10^3 |
|||
if caps then |
|||
spn_gb:wikitext('Ground') |
|||
num = num * 10^6 |
|||
elseif char == 'b' then |
|||
-- strip trailing letter |
|||
num = num * 10^9 |
|||
else |
else |
||
spn_gb:wikitext('ground') |
|||
return str |
|||
end |
end |
||
return num |
|||
else |
else |
||
spn_gb:wikitext(flr) |
|||
return str |
|||
:tag('sup') |
|||
:addClass('floornumber-ordinal-suffix') |
|||
:wikitext(ord_gb) |
|||
:done() |
|||
end |
end |
||
spn_gb:wikitext(' floor') |
|||
end |
|||
if not nohelp then |
|||
spn_gb:tag('sup') |
|||
function p.short( frame ) |
|||
:addClass('floornumber-help noexcerpt') |
|||
return p._short( frame.args[1] ) |
|||
:wikitext('[') |
|||
:tag('span') |
|||
:addClass('fact-text') |
|||
:addClass('floor-convention') |
|||
:attr('title', 'British convention; floor '..flr_us..' in the US') |
|||
:wikitext('UK') |
|||
:done() |
|||
:wikitext(']') |
|||
end |
|||
local spn_us = ret:tag('span') |
|||
spn_us :addClass('floornumber-us noexcerpt') |
|||
:wikitext(flr_us) |
|||
:tag('sup') |
|||
:addClass('floornumber-ordinal-suffix') |
|||
:wikitext(ord_us) |
|||
:done() |
|||
:wikitext(' floor') |
|||
:done() |
|||
if not nohelp then |
|||
local flr_gb |
|||
if flr == 0 then |
|||
flr_gb = 'ground floor' |
|||
else |
|||
flr_gb = 'floor '..flr |
|||
end |
|||
spn_us:tag('sup') |
|||
:addClass('floornumber-help noexcerpt') |
|||
:wikitext('[') |
|||
:tag('span') |
|||
:addClass('fact-text') |
|||
:addClass('floor-convention') |
|||
:attr('title', 'US convention; '..flr_gb..' in the UK') |
|||
:wikitext('US') |
|||
:done() |
|||
:wikitext(']') |
|||
end |
|||
return ret |
|||
end |
end |
||
Latest revision as of 11:22, 17 October 2024
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:FloorNumber/doc. [edit]
Module:FloorNumber's function main is invoked by Template:FloorNumber.
Module:FloorNumber requires Module:Ordinal.
Module:FloorNumber requires Module:Yesno.
local ordinal = require('Module:Ordinal')._main
local yn = require('Module:Yesno')
local p = {}
function p.main(frame)
return p._main(frame:getParent().args)
end
function p._main(args)
local nohelp = yn(args.nohelp)
local caps = yn(args.caps)
local flr = tonumber(args.uk)
-- prevent people from using anything but the main syntax for consistency across the wiki. Big red errors should make stuff clear.
assert(flr, "Missing parameter `uk`")
-- prevent unnamed parameters, which indicates people wanting to use deprecated |nohelp}}, |caps}}, or {{floornumber|1}} syntax.
assert(args[1] == nil, "Unrecognised parameter")
local flr_us = flr+1
local ord_gb = ordinal(flr, {nosup=true, nonum=true})
local ord_us = ordinal(flr_us, {nosup=true, nonum=true})
local ret = mw.html.create('span')
ret:addClass('floornumber')
local spn_gb = ret:tag('span')
spn_gb :addClass('floornumber-gb')
if flr == 0 then
if caps then
spn_gb:wikitext('Ground')
else
spn_gb:wikitext('ground')
end
else
spn_gb:wikitext(flr)
:tag('sup')
:addClass('floornumber-ordinal-suffix')
:wikitext(ord_gb)
:done()
end
spn_gb:wikitext(' floor')
if not nohelp then
spn_gb:tag('sup')
:addClass('floornumber-help noexcerpt')
:wikitext('[')
:tag('span')
:addClass('fact-text')
:addClass('floor-convention')
:attr('title', 'British convention; floor '..flr_us..' in the US')
:wikitext('UK')
:done()
:wikitext(']')
end
local spn_us = ret:tag('span')
spn_us :addClass('floornumber-us noexcerpt')
:wikitext(flr_us)
:tag('sup')
:addClass('floornumber-ordinal-suffix')
:wikitext(ord_us)
:done()
:wikitext(' floor')
:done()
if not nohelp then
local flr_gb
if flr == 0 then
flr_gb = 'ground floor'
else
flr_gb = 'floor '..flr
end
spn_us:tag('sup')
:addClass('floornumber-help noexcerpt')
:wikitext('[')
:tag('span')
:addClass('fact-text')
:addClass('floor-convention')
:attr('title', 'US convention; '..flr_gb..' in the UK')
:wikitext('US')
:done()
:wikitext(']')
end
return ret
end
return p