local p = local names =
-- two_decimals: boolean, whether to forbid a 1 d.p. result, so 2 becomes "2",-- 2.1 "2.10", and 2.123 "2.123"function p.decimal(val, length, two_decimals) local precision = math.max(0, math.ceil(length - 1 - math.log10(val))) if two_decimals and precision
function p.format(val, precision) local scale = math.floor(math.log10(val) / 3) - 1 if scale > 0 then scale = math.min(scale, #names) val = val / math.pow(10, 3 * (1 + scale)) return p.decimal(val, 3) .. '\194\160' .. names[scale] end
if precision <= 0 then val = math.floor(val / math.pow(10, -precision) + 0.5) val = val * math.pow(10, -precision) end return p.decimal(val, precision + 1, true)end
p[''] = function(frame) local val = tonumber(frame.args[1]) if not val or val < 0 then error('Format price error: cannot parse value "' .. (frame.args[1] or ) .. '"') end return p.format(val, tonumber(frame.args[2]) or 2)end
return p