webmcp

view framework/env/format/percentage.lua @ 405:c5f9a1b2f225

Updated year of copyright notice
author jbe
date Wed Jan 06 02:54:45 2016 +0100 (2016-01-06)
parents 9fdfb27f8e67
children
line source
1 --[[--
2 text = -- text with the value formatted as a percentage
3 format.percentage(
4 value, -- a number, a fraction or nil
5 {
6 nil_as = nil_text -- text to be returned for a nil value
7 digits = digits, -- digits before decimal point (of the percentage value)
8 precision = precision, -- digits after decimal point (of the percentage value)
9 decimal_shift = decimal_shift -- divide the value by 10^decimal_shift (setting true uses precision + 2)
10 }
11 )
13 Formats a number or fraction as a percentage.
15 --]]--
17 function format.percentage(value, options)
18 local options = table.new(options)
19 local f
20 if value == nil then
21 return options.nil_as or ""
22 elseif atom.has_type(value, atom.number) then
23 f = value
24 elseif atom.has_type(value, atom.fraction) then
25 f = value.float
26 else
27 error("Value passed to format.percentage(...) is neither a number nor a fraction nor nil.")
28 end
29 options.precision = options.precision or 0
30 if options.decimal_shift == true then
31 options.decimal_shift = options.precision + 2
32 end
33 local suffix = options.hide_unit and "" or " %"
34 return format.decimal(f * 100, options) .. suffix
35 end

Impressum / About Us