webmcp

annotate framework/env/format/currency.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 =
jbe/bsw@0 3 format.currency(
jbe/bsw@0 4 value,
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, -- number of digits before the decimal point
jbe/bsw@0 8 currency_precision = currency_precision, -- number of digits after decimal point
jbe/bsw@0 9 currency_prefix = currency_prefix, -- prefix string, i.e. "$ "
jbe/bsw@0 10 currency_decimal_point = currency_decimal_point, -- string to be used as decimal point
jbe/bsw@0 11 currency_suffix = currency_suffix, -- suffix string, i.e. " EUR"
jbe/bsw@0 12 hide_unit = hide_unit, -- hide the currency unit, if true
jbe/bsw@0 13 decimal_point = decimal_point -- used instead of 'currency_decimal_point', if 'hide_unit' is true
jbe/bsw@0 14 }
jbe/bsw@0 15 )
jbe/bsw@0 16
jbe/bsw@0 17 Formats a (floating point) number or a fraction as a decimal number. If a 'digits' option is set, the number of digits before the decimal point is increased up to the given count by preceding it with zeros. The digits after the decimal point are adjusted by the 'precision' parameter. The 'decimal_shift' parameter is useful, when fixed precision decimal numbers are stored as integers, as the given value will be divided by 10 to the power of the 'decimal_shift' value prior to formatting. Setting 'decimal_shift' to true will use the 'precision' value as 'decimal_shift'.
jbe/bsw@0 18
jbe/bsw@0 19 --]]--
jbe/bsw@0 20
jbe/bsw@0 21 function format.currency(value, options)
jbe/bsw@0 22 local options = table.new(options)
jbe/bsw@0 23 local prefix
jbe/bsw@0 24 local suffix
jbe/bsw@0 25 if options.hide_unit then
jbe/bsw@0 26 prefix = ""
jbe/bsw@0 27 suffix = ""
jbe/bsw@0 28 options.decimal_point =
jbe/bsw@0 29 options.decimal_point or locale.get("decimal_point")
jbe/bsw@0 30 options.precision =
jbe/bsw@0 31 options.currency_precision or locale.get("currency_precision") or 2
jbe/bsw@0 32 elseif
jbe/bsw@0 33 options.currency_prefix or options.currency_suffix or
jbe/bsw@0 34 options.currency_precision or options.currency_decimal_point
jbe/bsw@0 35 then
jbe/bsw@0 36 prefix = options.currency_prefix or ''
jbe/bsw@0 37 suffix = options.currency_suffix or ''
jbe/bsw@0 38 options.decimal_point = options.currency_decimal_point
jbe/bsw@0 39 options.precision = options.currency_precision or 2
jbe/bsw@0 40 else
jbe/bsw@0 41 prefix = locale.get("currency_prefix") or ''
jbe/bsw@0 42 suffix = locale.get("currency_suffix") or ''
jbe/bsw@0 43 options.decimal_point = locale.get("currency_decimal_point")
jbe/bsw@0 44 options.precision = locale.get("currency_precision") or 2
jbe/bsw@0 45 end
jbe/bsw@0 46 if value == nil then
jbe/bsw@0 47 return options.nil_as or ''
jbe/bsw@0 48 end
jbe/bsw@0 49 return prefix .. format.decimal(value, options) .. suffix
jbe/bsw@0 50 end

Impressum / About Us