Converts the given token into a decimal value. @param value value to be converted @param info input info (can be null) @param error raise error for an invalid input @return decimal, or null value is invalid and no error is raised @throws QueryException query exception
(final byte[] value, final InputInfo info,
final boolean error)
| 188 | * @throws QueryException query exception |
| 189 | */ |
| 190 | public static BigDecimal parse(final byte[] value, final InputInfo info, |
| 191 | final boolean error) throws QueryException { |
| 192 | |
| 193 | // check if conversion can be successful (reject exponential notation) |
| 194 | boolean valid = value.length != 0, dot = false; |
| 195 | if(valid) { |
| 196 | for(final byte v : value) { |
| 197 | if(v == '.') { |
| 198 | dot = true; |
| 199 | } else if(!(digit(v) || ws(v) || v == '+' || v == '-')) { |
| 200 | valid = false; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if(valid) { |
| 207 | // shortcut for non-fractional values |
| 208 | if(!dot) { |
| 209 | final long l = toLong(value); |
| 210 | if(l != Long.MIN_VALUE) return BigDecimal.valueOf(l); |
| 211 | } |
| 212 | // decimal conversion |
| 213 | try { |
| 214 | return new BigDecimal(Token.string(value).trim()); |
| 215 | } catch(final NumberFormatException ex) { |
| 216 | Util.debug(ex); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if(error) throw BasicType.DECIMAL.castError(value, info); |
| 221 | return null; |
| 222 | } |
| 223 | } |