webmcp
diff framework/env/parse/percentage.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/percentage.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,30 @@ 1.4 +function parse.percentage(str, dest_type, options) 1.5 + local str = parse._pre_fold(str) 1.6 + local dest_type = dest_type or atom.number 1.7 + local options = table.new(options) 1.8 + options.precision = options.precision or 0 1.9 + if options.decimal_shift == true then 1.10 + options.decimal_shift = options.precision + 2 1.11 + end 1.12 + local f = parse.decimal(string.match(str, "^ *([^%%]*) *%%? *$"), dest_type, options) 1.13 + if dest_type == atom.number then 1.14 + if f then 1.15 + return f / 100 1.16 + end 1.17 + elseif dest_type == atom.integer then 1.18 + if f then 1.19 + f = f / 100 1.20 + if atom.is_integer(f) then 1.21 + return f 1.22 + else 1.23 + return atom.integer.invalid 1.24 + end 1.25 + end 1.26 + elseif dest_type == atom.fraction then 1.27 + if f then 1.28 + return f / 100 1.29 + end 1.30 + else 1.31 + error("Missing or invalid destination type for parsing.") 1.32 + end 1.33 +end