jbe/bsw@0: function parse.percentage(str, dest_type, options) jbe/bsw@0: local str = parse._pre_fold(str) jbe/bsw@0: local dest_type = dest_type or atom.number jbe/bsw@0: local options = table.new(options) jbe/bsw@0: options.precision = options.precision or 0 jbe/bsw@0: if options.decimal_shift == true then jbe/bsw@0: options.decimal_shift = options.precision + 2 jbe/bsw@0: end jbe/bsw@0: local f = parse.decimal(string.match(str, "^ *([^%%]*) *%%? *$"), dest_type, options) jbe/bsw@0: if dest_type == atom.number then jbe/bsw@0: if f then jbe/bsw@0: return f / 100 jbe/bsw@0: end jbe/bsw@0: elseif dest_type == atom.integer then jbe/bsw@0: if f then jbe/bsw@0: f = f / 100 jbe/bsw@0: if atom.is_integer(f) then jbe/bsw@0: return f jbe/bsw@0: else jbe/bsw@0: return atom.integer.invalid jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: elseif dest_type == atom.fraction then jbe/bsw@0: if f then jbe/bsw@0: return f / 100 jbe/bsw@0: end jbe/bsw@0: else jbe/bsw@0: error("Missing or invalid destination type for parsing.") jbe/bsw@0: end jbe/bsw@0: end