Module:Convert: Difference between revisions

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)
(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:
local function extract_fraction(parms, text, negative)
-- If text represents a fraction, return
-- value, altvalue, show, spelled, denominator
-- where
-- 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,
-- and is spelled if wanted and possible)
Line 1,051 ⟶ 1,053:
-- (which may be negative) are also accepted (like old template).
-- Template interprets '1.23e+2+12/24' as '123(12/24)' = 123.5!
local numstr, whole, value, altvalue
local lhs, slash, denstr = text:match('^%s*([^/]-)%s*(/+)%s*(.-)%s*$')
local denominator = tonumber(denstr)
Line 1,072 ⟶ 1,074:
if negative == negfrac or wholestr == nil then
value = whole + numerator / denominator
altvalue = whole + numerator / (denominator * 10)
do_spell = parms.opt_spell_in
if do_spell then
Line 1,080 ⟶ 1,083:
else
value = whole - numerator / denominator
altvalue = whole - numerator / (denominator * 10)
numstr = change_sign(numstr)
do_spell = false
Line 1,110 ⟶ 1,114:
wikitext = spell_number(parms, wholestr, numsign .. numstr, denstr) or wikitext
end
return value, altvalue, wikitext, do_spell, denominator
end
 
Line 1,126 ⟶ 1,130:
-- If successful, the returned info table contains named fields:
-- 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)
-- = false if value is -1 (like old template)
Line 1,146 ⟶ 1,152:
local singular, show, denominator
local value = tonumber(clean)
local altvalue
if value then
local sign = clean:sub(1, 1)
Line 1,176 ⟶ 1,183:
local spelled
if not no_fraction then
value, altvalue, show, spelled, denominator = extract_fraction(parms, clean, isnegative)
end
if value == nil then
Line 1,188 ⟶ 1,195:
end
end
if not valid_number(value) then -- for example, "1e310" overflowsmay overflow
return false, { 'cvt_invalid_num' }
end
Line 1,211 ⟶ 1,218:
return true, {
value = value,
altvalue = altvalue or value,
singular = singular,
clean = clean,
Line 1,828 ⟶ 1,836:
if info then
invalue, inclean = info.value, info.clean
if in_current.builtin == 'hand' then
invalue = info.altvalue
end
end
if invalue == nil or invalue == '' then
Anonymous user