webmcp

view framework/env/parse/currency.lua @ 406:36f0d1d5ed7d

Further fixes in mondelefant.connect{...} (including proper handling of garbage collection in case of memory allocation errors); Code cleanup (use luaL_setmetatable, which is available since Lua 5.2)
author jbe
date Wed Jan 06 18:59:58 2016 +0100 (2016-01-06)
parents 9fdfb27f8e67
children
line source
1 local function extract_numbers(str)
2 local result = {}
3 for char in string.gmatch(str, "[0-9]") do
4 result[#result+1] = char
5 end
6 return table.concat(result)
7 end
9 function parse.currency(str, dest_type, options)
10 local str = parse._pre_fold(str)
11 local dest_type = dest_type or atom.number
12 local options = options or {}
13 local currency_decimal_point = locale.get("currency_decimal_point")
14 local decimal_point = locale.get("decimal_point") or "."
15 local pos1, pos2
16 if currency_decimal_point then
17 pos1, pos2 = string.find(str, currency_decimal_point, 1, true)
18 end
19 if not pos1 then
20 pos1, pos2 = string.find(str, decimal_point, 1, true)
21 end
22 if pos1 then
23 local p1 = extract_numbers(string.sub(str, 1, pos1 - 1))
24 local p2 = extract_numbers(string.sub(str, pos2 + 1, #str))
25 return parse.decimal(p1 .. decimal_point .. p2, dest_type, options)
26 else
27 return parse.decimal(extract_numbers(str), dest_type, options)
28 end
29 end

Impressum / About Us