Module:Convert/sandbox explained

-- Convert a value from one unit of measurement to another.-- Example: 123lb --> 123 pounds (56 kg)-- See if copying to another wiki.

local MINUS = '−' -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)local abs = math.abslocal floor = math.floorlocal format = string.formatlocal log10 = math.log10local ustring = mw.ustringlocal ulen = ustring.lenlocal usub = ustring.sub

-- Configuration options to keep magic values in one location.-- Conversion data and message text are defined in separate modules.local config, maxsigfiglocal numdot -- must be '.' or ',' or a character which works in a regexlocal numsep, numsep_remove, numsep_remove2local data_code, all_unitslocal text_codelocal varname -- can be a code to use variable names that depend on valuelocal from_en_table -- to translate an output string of en digits to local languagelocal to_en_table -- to translate an input string of digits in local language to en-- Use translation_table in convert/text to change the following.local en_default -- true uses lang=en unless convert has lang=local or local digitslocal group_method = 3 -- code for how many digits are in a grouplocal per_word = 'per' -- for units like "liters per kilometer"local plural_suffix = 's' -- only other useful value is probably to disable plural unit nameslocal omitsep -- true to omit separator before local symbol/name

-- All units should be defined in the data module. However, to cater for quick changes-- and experiments, any unknown unit is looked up in an extra data module, if it exists.-- That module would be transcluded in only a small number of pages, so there should be-- little server overhead from making changes, and changes should propagate quickly.local extra_module -- name of module with extra unitslocal extra_units -- nil or table of extra units from extra_module

-- Some options in the invoking template can set variables used later in the module.local currency_text -- for a user-defined currency symbol: 12$/ha (euro replaces dollar)

local function from_en(text) -- Input is a string representing a number in en digits with '.' decimal mark, -- without digit grouping (which is done just after calling this). -- Return the translation of the string with numdot and digits in local language. if numdot ~= '.' then text = text:gsub('%.', numdot) end if from_en_table then text = text:gsub('%d', from_en_table) end return textend

local function to_en(text) -- Input is a string representing a number in the local language with -- an optional numdot decimal mark and numsep digit grouping. -- Return the translation of the string with '.' mark and en digits, -- and no separators (they have to be removed here to handle cases like -- numsep = '.' and numdot = ',' with input "1.234.567,8"). if to_en_table then text = ustring.gsub(text, '%d', to_en_table) end if numsep_remove then text = text:gsub(numsep_remove, ) end if numsep_remove2 then text = text:gsub(numsep_remove2, ) end if numdot ~= '.' then text = text:gsub(numdot, '.') end return textend

local function decimal_mark(text) -- Return ',' if text probably is using comma for decimal mark, or has no decimal mark. -- Return '.' if text probably is using dot for decimal mark. -- Otherwise return nothing (decimal mark not known). if not text:find('[.,]') then return ',' end text = text:gsub('^%-', ):gsub('%+%d+/%d+$', ):gsub('[Ee]%-?%d+$', ) local decimal = text:match('^0?([.,])%d+$') or text:match('%d([.,])%d?%d?$') or text:match('%d([.,])%d%d%d%d+$') if decimal then return decimal end if text:match('%.%d+%.') then return ',' end if text:match('%,%d+,') then return '.' endend

local add_warning, with_separator -- forward declarationslocal function to_en_with_check(text, parms) -- Version of to_en for a wiki using numdot = ',' and numsep = '.' to check -- text (an input number as a string) which might have been copied from enwiki. -- For example, in '1.234' the '.' could be a decimal mark or a group separator. -- From viwiki. if to_en_table then text = ustring.gsub(text, '%d', to_en_table) end if decimal_mark(text)

'.' then local original = text text = text:gsub(',', ) -- for example, interpret "1,234.5" as an enwiki value if parms then add_warning(parms, 0, 'cvt_enwiki_num', original, with_separator(text)) end else if numsep_remove then text = text:gsub(numsep_remove, ) end if numsep_remove2 then text = text:gsub(numsep_remove2, ) end if numdot ~= '.' then text = text:gsub(numdot, '.') end end return textend

local function omit_separator(id) -- Return true if there should be no separator before id (a unit symbol or name). -- For zhwiki, there should be no separator if id uses local characters. -- The following kludge should be a sufficient test. if omitsep then if id:sub(1, 2)