webmcp

diff framework/env/format/currency.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/format/currency.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,50 @@
     1.4 +--[[--
     1.5 +text =
     1.6 +format.currency(
     1.7 +  value,
     1.8 +  {
     1.9 +    nil_as                 = nil_text,                -- text to be returned for a nil value
    1.10 +    digits                 = digits,                  -- number of digits before the decimal point
    1.11 +    currency_precision     = currency_precision,      -- number of digits after decimal point
    1.12 +    currency_prefix        = currency_prefix,         -- prefix string, i.e. "$ "
    1.13 +    currency_decimal_point = currency_decimal_point,  -- string to be used as decimal point
    1.14 +    currency_suffix        = currency_suffix,         -- suffix string, i.e. " EUR"
    1.15 +    hide_unit              = hide_unit,               -- hide the currency unit, if true
    1.16 +    decimal_point          = decimal_point            -- used instead of 'currency_decimal_point', if 'hide_unit' is true
    1.17 +  }
    1.18 +)
    1.19 +
    1.20 +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'.
    1.21 +
    1.22 +--]]--
    1.23 +
    1.24 +function format.currency(value, options)
    1.25 +  local options = table.new(options)
    1.26 +  local prefix
    1.27 +  local suffix
    1.28 +  if options.hide_unit then
    1.29 +    prefix = ""
    1.30 +    suffix = ""
    1.31 +    options.decimal_point =
    1.32 +      options.decimal_point or locale.get("decimal_point")
    1.33 +    options.precision =
    1.34 +      options.currency_precision or locale.get("currency_precision") or 2
    1.35 +  elseif
    1.36 +    options.currency_prefix or options.currency_suffix or
    1.37 +    options.currency_precision or options.currency_decimal_point
    1.38 +  then
    1.39 +    prefix                = options.currency_prefix or ''
    1.40 +    suffix                = options.currency_suffix or ''
    1.41 +    options.decimal_point = options.currency_decimal_point
    1.42 +    options.precision     = options.currency_precision or 2
    1.43 +  else
    1.44 +    prefix                = locale.get("currency_prefix") or ''
    1.45 +    suffix                = locale.get("currency_suffix") or ''
    1.46 +    options.decimal_point = locale.get("currency_decimal_point")
    1.47 +    options.precision     = locale.get("currency_precision") or 2
    1.48 +  end
    1.49 +  if value == nil then
    1.50 +    return options.nil_as or ''
    1.51 +  end
    1.52 +  return prefix .. format.decimal(value, options) .. suffix
    1.53 +end

Impressum / About Us