Module:Convert and Module:Convert/sandbox: Difference between pages
(Difference between pages)
Polasciana (talk | contribs) m 1 revision imported |
Polasciana (talk | contribs) m 1 revision imported |
||
| Line 1,560: | Line 1,560: | ||
end | end | ||
local function fraction_table(value, denominator) | local function fraction_table(parms, value, denominator) | ||
-- Return value as a string or a table: | -- Return value as a string or a table: | ||
-- * If result is a string, there is no fraction, and the result | -- * If result is a string, there is no fraction, and the result | ||
| Line 1,566: | Line 1,566: | ||
-- * If result is a table, it represents a fraction with named fields: | -- * If result is a table, it represents a fraction with named fields: | ||
-- wholestr, numstr, denstr (strings of en digits for integer, numerator, denominator). | -- wholestr, numstr, denstr (strings of en digits for integer, numerator, denominator). | ||
-- The result is rounded to the nearest multiple of (1/denominator). | -- The result is rounded to the nearest multiple of (1/denominator), or | ||
-- optionally is rounded down. | |||
-- If the multiple is zero, no fraction is included. | -- If the multiple is zero, no fraction is included. | ||
-- No fraction is included if value is very large as the fraction would | -- No fraction is included if value is very large as the fraction would | ||
| Line 1,580: | Line 1,581: | ||
local integer, decimals = math.modf(value) | local integer, decimals = math.modf(value) | ||
local numerator = floor((decimals * denominator) + | local numerator = floor((decimals * denominator) + | ||
0.5 + 2e-14) -- add fudge for some common cases of bad rounding | (parms.opt_round_down and 0 or 0.5) + | ||
2e-14) -- add fudge for some common cases of bad rounding | |||
if numerator >= denominator then | if numerator >= denominator then | ||
integer = integer + 1 | integer = integer + 1 | ||
| Line 2,587: | Line 2,589: | ||
local denominator = out_current.frac | local denominator = out_current.frac | ||
if denominator then | if denominator then | ||
show = fraction_table(outvalue, denominator) | show = fraction_table(parms, outvalue, denominator) | ||
else | else | ||
precision = parms.precision | precision = parms.precision | ||