webmcp
annotate 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 | 
| 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 |