webmcp

annotate framework/env/format/percentage.lua @ 382:810c020b0da4

Reverted changes to mondelefant_result_index and mondelefant_result_newindex C-functions (proxying of JSON document)
author jbe
date Mon Nov 16 18:31:49 2015 +0100 (2015-11-16)
parents 9fdfb27f8e67
children
rev   line source
jbe/bsw@0 1 --[[--
jbe/bsw@0 2 text = -- text with the value formatted as a percentage
jbe/bsw@0 3 format.percentage(
jbe/bsw@0 4 value, -- a number, a fraction or nil
jbe/bsw@0 5 {
jbe/bsw@0 6 nil_as = nil_text -- text to be returned for a nil value
jbe/bsw@0 7 digits = digits, -- digits before decimal point (of the percentage value)
jbe/bsw@0 8 precision = precision, -- digits after decimal point (of the percentage value)
jbe/bsw@0 9 decimal_shift = decimal_shift -- divide the value by 10^decimal_shift (setting true uses precision + 2)
jbe/bsw@0 10 }
jbe/bsw@0 11 )
jbe/bsw@0 12
jbe/bsw@0 13 Formats a number or fraction as a percentage.
jbe/bsw@0 14
jbe/bsw@0 15 --]]--
jbe/bsw@0 16
jbe/bsw@0 17 function format.percentage(value, options)
jbe/bsw@0 18 local options = table.new(options)
jbe/bsw@0 19 local f
jbe/bsw@0 20 if value == nil then
jbe/bsw@0 21 return options.nil_as or ""
jbe/bsw@0 22 elseif atom.has_type(value, atom.number) then
jbe/bsw@0 23 f = value
jbe/bsw@0 24 elseif atom.has_type(value, atom.fraction) then
jbe/bsw@0 25 f = value.float
jbe/bsw@0 26 else
jbe/bsw@0 27 error("Value passed to format.percentage(...) is neither a number nor a fraction nor nil.")
jbe/bsw@0 28 end
jbe/bsw@0 29 options.precision = options.precision or 0
jbe/bsw@0 30 if options.decimal_shift == true then
jbe/bsw@0 31 options.decimal_shift = options.precision + 2
jbe/bsw@0 32 end
jbe/bsw@0 33 local suffix = options.hide_unit and "" or " %"
jbe/bsw@0 34 return format.decimal(f * 100, options) .. suffix
jbe/bsw@0 35 end

Impressum / About Us