MCPcopy Create free account
hub / github.com/BaseXdb/basex / parse

Method parse

basex-core/src/main/java/org/basex/query/value/item/Dec.java:190–222  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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}

Callers 3

numberLitMethod · 0.95
compareMethod · 0.95
decMethod · 0.95

Calls 7

stringMethod · 0.95
debugMethod · 0.95
castErrorMethod · 0.80
digitMethod · 0.45
wsMethod · 0.45
toLongMethod · 0.45
trimMethod · 0.45

Tested by

no test coverage detected