annotate framework/env/parse/currency.lua @ 510:696d7e5f2bcb
Added "Layouts" section to WebMCP documentation and cleaned "Directory structure" section up
 | author | 
 jbe | 
 | date | 
 Mon Aug 21 03:58:33 2017 +0200 (2017-08-21) | 
 | parents | 
 9fdfb27f8e67  | 
 | children | 
  | 
 
 | rev | 
   line source | 
| 
jbe/bsw@0
 | 
     1 local function extract_numbers(str)
 | 
| 
jbe/bsw@0
 | 
     2   local result = {}
 | 
| 
jbe/bsw@0
 | 
     3   for char in string.gmatch(str, "[0-9]") do
 | 
| 
jbe/bsw@0
 | 
     4     result[#result+1] = char
 | 
| 
jbe/bsw@0
 | 
     5   end
 | 
| 
jbe/bsw@0
 | 
     6   return table.concat(result)
 | 
| 
jbe/bsw@0
 | 
     7 end
 | 
| 
jbe/bsw@0
 | 
     8 
 | 
| 
jbe/bsw@0
 | 
     9 function parse.currency(str, dest_type, options)
 | 
| 
jbe/bsw@0
 | 
    10   local str = parse._pre_fold(str)
 | 
| 
jbe/bsw@0
 | 
    11   local dest_type = dest_type or atom.number
 | 
| 
jbe/bsw@0
 | 
    12   local options = options or {}
 | 
| 
jbe/bsw@0
 | 
    13   local currency_decimal_point = locale.get("currency_decimal_point")
 | 
| 
jbe/bsw@0
 | 
    14   local decimal_point = locale.get("decimal_point") or "."
 | 
| 
jbe/bsw@0
 | 
    15   local pos1, pos2
 | 
| 
jbe/bsw@0
 | 
    16   if currency_decimal_point then
 | 
| 
jbe/bsw@0
 | 
    17     pos1, pos2 = string.find(str, currency_decimal_point, 1, true)
 | 
| 
jbe/bsw@0
 | 
    18   end
 | 
| 
jbe/bsw@0
 | 
    19   if not pos1 then
 | 
| 
jbe/bsw@0
 | 
    20     pos1, pos2 = string.find(str, decimal_point, 1, true)
 | 
| 
jbe/bsw@0
 | 
    21   end
 | 
| 
jbe/bsw@0
 | 
    22   if pos1 then
 | 
| 
jbe/bsw@0
 | 
    23     local p1 = extract_numbers(string.sub(str, 1, pos1 - 1))
 | 
| 
jbe/bsw@0
 | 
    24     local p2 = extract_numbers(string.sub(str, pos2 + 1, #str))
 | 
| 
jbe/bsw@0
 | 
    25     return parse.decimal(p1 .. decimal_point .. p2, dest_type, options)
 | 
| 
jbe/bsw@0
 | 
    26   else
 | 
| 
jbe/bsw@0
 | 
    27     return parse.decimal(extract_numbers(str), dest_type, options)
 | 
| 
jbe/bsw@0
 | 
    28   end
 | 
| 
jbe/bsw@0
 | 
    29 end
 |