webmcp
view framework/env/format/currency.lua @ 400:7b183a70a37d
Bugfix in "document_column" write mechanism: write to document field when column does NOT exist
| author | jbe | 
|---|---|
| date | Mon Jan 04 00:42:28 2016 +0100 (2016-01-04) | 
| parents | 9fdfb27f8e67 | 
| children | 
 line source
     1 --[[--
     2 text =
     3 format.currency(
     4   value,
     5   {
     6     nil_as                 = nil_text,                -- text to be returned for a nil value
     7     digits                 = digits,                  -- number of digits before the decimal point
     8     currency_precision     = currency_precision,      -- number of digits after decimal point
     9     currency_prefix        = currency_prefix,         -- prefix string, i.e. "$ "
    10     currency_decimal_point = currency_decimal_point,  -- string to be used as decimal point
    11     currency_suffix        = currency_suffix,         -- suffix string, i.e. " EUR"
    12     hide_unit              = hide_unit,               -- hide the currency unit, if true
    13     decimal_point          = decimal_point            -- used instead of 'currency_decimal_point', if 'hide_unit' is true
    14   }
    15 )
    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'.
    19 --]]--
    21 function format.currency(value, options)
    22   local options = table.new(options)
    23   local prefix
    24   local suffix
    25   if options.hide_unit then
    26     prefix = ""
    27     suffix = ""
    28     options.decimal_point =
    29       options.decimal_point or locale.get("decimal_point")
    30     options.precision =
    31       options.currency_precision or locale.get("currency_precision") or 2
    32   elseif
    33     options.currency_prefix or options.currency_suffix or
    34     options.currency_precision or options.currency_decimal_point
    35   then
    36     prefix                = options.currency_prefix or ''
    37     suffix                = options.currency_suffix or ''
    38     options.decimal_point = options.currency_decimal_point
    39     options.precision     = options.currency_precision or 2
    40   else
    41     prefix                = locale.get("currency_prefix") or ''
    42     suffix                = locale.get("currency_suffix") or ''
    43     options.decimal_point = locale.get("currency_decimal_point")
    44     options.precision     = locale.get("currency_precision") or 2
    45   end
    46   if value == nil then
    47     return options.nil_as or ''
    48   end
    49   return prefix .. format.decimal(value, options) .. suffix
    50 end
