Module:JI ratio finder: Difference between revisions

Ganaram inukshuk (talk | contribs)
m Fixed delimiter-adding code
Ganaram inukshuk (talk | contribs)
Added option to add error to text output
Line 225: Line 225:
for i = 1, #ratios do
for i = 1, #ratios do
text = text .. rat.as_ratio(ratios[i])
text = text .. rat.as_ratio(ratios[i])
if i < #ratios then
text = text .. delimiter
end
end
return text
end
-- Converts ratios to text, with delimiter
-- Default delimiter is a comma followed by a space
function p.ratios_to_text_with_error(ratios, target_cents, delimiter, add_links)
local ratios = ratios or { rat.new(5, 4), rat.new (81, 64) }
local target_cents = target_cents
local delimiter = delimiter or ", "
local add_links = add_links == true
local text = ""
for i = 1, #ratios do
local ratio_as_text  = rat.as_ratio(ratios[i])
local ratio_as_cents = rat.as_ratio(ratios[i])
local diff = ratio_as_cents - target_cents
if add_links then
text = text .. string.format('[[%s]]', ratio_as_text)
else
text = text .. ratio_as_text
end
if diff > 0 then
text = text .. string.format(' (+%.3f)')
elseif diff < 0 then
text = text .. string.format(' (%.3f)')
elseif diff == 0 then
text = text .. "(just)"
end
if i < #ratios then
if i < #ratios then
text = text .. delimiter
text = text .. delimiter