webmcp

view framework/env/parse/percentage.lua @ 412:7d43be9afa56

Improved memory cleanup in case of out-of-memory errors (PQnotifies and PQunescapeBytea)
author jbe
date Fri Jan 08 03:10:33 2016 +0100 (2016-01-08)
parents 9fdfb27f8e67
children
line source
1 function parse.percentage(str, dest_type, options)
2 local str = parse._pre_fold(str)
3 local dest_type = dest_type or atom.number
4 local options = table.new(options)
5 options.precision = options.precision or 0
6 if options.decimal_shift == true then
7 options.decimal_shift = options.precision + 2
8 end
9 local f = parse.decimal(string.match(str, "^ *([^%%]*) *%%? *$"), dest_type, options)
10 if dest_type == atom.number then
11 if f then
12 return f / 100
13 end
14 elseif dest_type == atom.integer then
15 if f then
16 f = f / 100
17 if atom.is_integer(f) then
18 return f
19 else
20 return atom.integer.invalid
21 end
22 end
23 elseif dest_type == atom.fraction then
24 if f then
25 return f / 100
26 end
27 else
28 error("Missing or invalid destination type for parsing.")
29 end
30 end

Impressum / About Us