Module:Convert: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(for a "per" unit, use any specified link rather than constructing it) |
(Theodore O'Connor was a {{hands|14.1+3/4}} pony means 14 hands + 1.75 inches = 57.75 inches (although module rounds that to 57.8 by default)) |
||
Line 1,027: | Line 1,027: | ||
local function extract_fraction(parms, text, negative) |
local function extract_fraction(parms, text, negative) |
||
-- If text represents a fraction, return |
-- If text represents a fraction, return |
||
-- value, show, spelled, denominator |
-- value, altvalue, show, spelled, denominator |
||
-- where |
-- where |
||
-- value is a number (value of the fraction in argument text) |
-- value is a number (value of the fraction in argument text) |
||
-- altvalue is an alternate interpretation of any fraction for the hands |
|||
-- unit where "14.1+3/4" means 14 hands 1.75 inches! |
|||
-- show is a string (formatted text for display of an input value, |
-- show is a string (formatted text for display of an input value, |
||
-- and is spelled if wanted and possible) |
-- and is spelled if wanted and possible) |
||
Line 1,051: | Line 1,053: | ||
-- (which may be negative) are also accepted (like old template). |
-- (which may be negative) are also accepted (like old template). |
||
-- Template interprets '1.23e+2+12/24' as '123(12/24)' = 123.5! |
-- Template interprets '1.23e+2+12/24' as '123(12/24)' = 123.5! |
||
local numstr, whole, value |
local numstr, whole, value, altvalue |
||
local lhs, slash, denstr = text:match('^%s*([^/]-)%s*(/+)%s*(.-)%s*$') |
local lhs, slash, denstr = text:match('^%s*([^/]-)%s*(/+)%s*(.-)%s*$') |
||
local denominator = tonumber(denstr) |
local denominator = tonumber(denstr) |
||
Line 1,072: | Line 1,074: | ||
if negative == negfrac or wholestr == nil then |
if negative == negfrac or wholestr == nil then |
||
value = whole + numerator / denominator |
value = whole + numerator / denominator |
||
altvalue = whole + numerator / (denominator * 10) |
|||
do_spell = parms.opt_spell_in |
do_spell = parms.opt_spell_in |
||
if do_spell then |
if do_spell then |
||
Line 1,080: | Line 1,083: | ||
else |
else |
||
value = whole - numerator / denominator |
value = whole - numerator / denominator |
||
altvalue = whole - numerator / (denominator * 10) |
|||
numstr = change_sign(numstr) |
numstr = change_sign(numstr) |
||
do_spell = false |
do_spell = false |
||
Line 1,110: | Line 1,114: | ||
wikitext = spell_number(parms, wholestr, numsign .. numstr, denstr) or wikitext |
wikitext = spell_number(parms, wholestr, numsign .. numstr, denstr) or wikitext |
||
end |
end |
||
return value, wikitext, do_spell, denominator |
return value, altvalue, wikitext, do_spell, denominator |
||
end |
end |
||
Line 1,126: | Line 1,130: | ||
-- If successful, the returned info table contains named fields: |
-- If successful, the returned info table contains named fields: |
||
-- value = a valid number |
-- value = a valid number |
||
-- altvalue = a valid number, usually same as value but different |
|||
-- if fraction used (for hands unit) |
|||
-- singular = true if value is 1 (to use singular form of units) |
-- singular = true if value is 1 (to use singular form of units) |
||
-- = false if value is -1 (like old template) |
-- = false if value is -1 (like old template) |
||
Line 1,146: | Line 1,152: | ||
local singular, show, denominator |
local singular, show, denominator |
||
local value = tonumber(clean) |
local value = tonumber(clean) |
||
local altvalue |
|||
if value then |
if value then |
||
local sign = clean:sub(1, 1) |
local sign = clean:sub(1, 1) |
||
Line 1,176: | Line 1,183: | ||
local spelled |
local spelled |
||
if not no_fraction then |
if not no_fraction then |
||
value, show, spelled, denominator = extract_fraction(parms, clean, isnegative) |
value, altvalue, show, spelled, denominator = extract_fraction(parms, clean, isnegative) |
||
end |
end |
||
if value == nil then |
if value == nil then |
||
Line 1,188: | Line 1,195: | ||
end |
end |
||
end |
end |
||
if not valid_number(value) then -- for example, "1e310" |
if not valid_number(value) then -- for example, "1e310" may overflow |
||
return false, { 'cvt_invalid_num' } |
return false, { 'cvt_invalid_num' } |
||
end |
end |
||
Line 1,211: | Line 1,218: | ||
return true, { |
return true, { |
||
value = value, |
value = value, |
||
altvalue = altvalue or value, |
|||
singular = singular, |
singular = singular, |
||
clean = clean, |
clean = clean, |
||
Line 1,828: | Line 1,836: | ||
if info then |
if info then |
||
invalue, inclean = info.value, info.clean |
invalue, inclean = info.value, info.clean |
||
if in_current.builtin == 'hand' then |
|||
invalue = info.altvalue |
|||
end |
|||
end |
end |
||
if invalue == nil or invalue == '' then |
if invalue == nil or invalue == '' then |