webmcp

diff framework/env/parse/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/parse/currency.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,29 @@
     1.4 +local function extract_numbers(str)
     1.5 +  local result = {}
     1.6 +  for char in string.gmatch(str, "[0-9]") do
     1.7 +    result[#result+1] = char
     1.8 +  end
     1.9 +  return table.concat(result)
    1.10 +end
    1.11 +
    1.12 +function parse.currency(str, dest_type, options)
    1.13 +  local str = parse._pre_fold(str)
    1.14 +  local dest_type = dest_type or atom.number
    1.15 +  local options = options or {}
    1.16 +  local currency_decimal_point = locale.get("currency_decimal_point")
    1.17 +  local decimal_point = locale.get("decimal_point") or "."
    1.18 +  local pos1, pos2
    1.19 +  if currency_decimal_point then
    1.20 +    pos1, pos2 = string.find(str, currency_decimal_point, 1, true)
    1.21 +  end
    1.22 +  if not pos1 then
    1.23 +    pos1, pos2 = string.find(str, decimal_point, 1, true)
    1.24 +  end
    1.25 +  if pos1 then
    1.26 +    local p1 = extract_numbers(string.sub(str, 1, pos1 - 1))
    1.27 +    local p2 = extract_numbers(string.sub(str, pos2 + 1, #str))
    1.28 +    return parse.decimal(p1 .. decimal_point .. p2, dest_type, options)
    1.29 +  else
    1.30 +    return parse.decimal(extract_numbers(str), dest_type, options)
    1.31 +  end
    1.32 +end

Impressum / About Us