jbe/bsw@0: --[[-- jbe/bsw@0: text = jbe/bsw@0: format.currency( jbe/bsw@0: value, jbe/bsw@0: { jbe/bsw@0: nil_as = nil_text, -- text to be returned for a nil value jbe/bsw@0: digits = digits, -- number of digits before the decimal point jbe/bsw@0: currency_precision = currency_precision, -- number of digits after decimal point jbe/bsw@0: currency_prefix = currency_prefix, -- prefix string, i.e. "$ " jbe/bsw@0: currency_decimal_point = currency_decimal_point, -- string to be used as decimal point jbe/bsw@0: currency_suffix = currency_suffix, -- suffix string, i.e. " EUR" jbe/bsw@0: hide_unit = hide_unit, -- hide the currency unit, if true jbe/bsw@0: decimal_point = decimal_point -- used instead of 'currency_decimal_point', if 'hide_unit' is true jbe/bsw@0: } jbe/bsw@0: ) jbe/bsw@0: jbe/bsw@0: 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: jbe/bsw@0: --]]-- jbe/bsw@0: jbe/bsw@0: function format.currency(value, options) jbe/bsw@0: local options = table.new(options) jbe/bsw@0: local prefix jbe/bsw@0: local suffix jbe/bsw@0: if options.hide_unit then jbe/bsw@0: prefix = "" jbe/bsw@0: suffix = "" jbe/bsw@0: options.decimal_point = jbe/bsw@0: options.decimal_point or locale.get("decimal_point") jbe/bsw@0: options.precision = jbe/bsw@0: options.currency_precision or locale.get("currency_precision") or 2 jbe/bsw@0: elseif jbe/bsw@0: options.currency_prefix or options.currency_suffix or jbe/bsw@0: options.currency_precision or options.currency_decimal_point jbe/bsw@0: then jbe/bsw@0: prefix = options.currency_prefix or '' jbe/bsw@0: suffix = options.currency_suffix or '' jbe/bsw@0: options.decimal_point = options.currency_decimal_point jbe/bsw@0: options.precision = options.currency_precision or 2 jbe/bsw@0: else jbe/bsw@0: prefix = locale.get("currency_prefix") or '' jbe/bsw@0: suffix = locale.get("currency_suffix") or '' jbe/bsw@0: options.decimal_point = locale.get("currency_decimal_point") jbe/bsw@0: options.precision = locale.get("currency_precision") or 2 jbe/bsw@0: end jbe/bsw@0: if value == nil then jbe/bsw@0: return options.nil_as or '' jbe/bsw@0: end jbe/bsw@0: return prefix .. format.decimal(value, options) .. suffix jbe/bsw@0: end