List of usage examples for org.apache.commons.lang.math NumberUtils createNumber
public static Number createNumber(String str) throws NumberFormatException
Turns a string value into a java.lang.Number.
First, the value is examined for a type qualifier on the end ('f','F','d','D','l','L').
From source file:com.fengduo.bee.commons.util.NumberParser.java
public static Number parserNumber(String number) { try {//from ww w. j ava 2 s. c o m return NumberUtils.createNumber(number); } catch (NumberFormatException e) { return null; } }
From source file:com.qualogy.qafe.core.script.Script.java
public void addAll(ApplicationContext context, ApplicationStoreIdentifier storeId, DataIdentifier dataId, List placeHolders, String localStoreId) { for (Iterator iter = placeHolders.iterator(); iter.hasNext();) { PlaceHolder placeHolder = (PlaceHolder) iter.next(); if (placeHolder != null) { Object placeHolderValue = ParameterValueHandler.get(context, storeId, dataId, placeHolder, localStoreId);/*from ww w .j a v a2 s. co m*/ if (placeHolderValue instanceof String && NumberUtils.isNumber((String) placeHolderValue)) { placeHolderValue = NumberUtils.createNumber((String) placeHolderValue); } if (placeHolderValue instanceof BigDecimal) { placeHolderValue = new Double(((BigDecimal) placeHolderValue).doubleValue()); } String scriptKey = placeHolder.getName(); scriptKey = IDENTIFIER_PREFIX + parameters.keySet().size(); expression = StringUtils.replace(expression, placeHolder.getPlaceHolderKey(), scriptKey); parameters.put(scriptKey, placeHolderValue); } } }
From source file:com.cosmosource.common.action.CALicenseAction.java
/** * ca????// w w w . j a v a 2 s . c o m * @return */ public String license() { entity.setGencauser(getSession().getAttribute(Constants.USER_NAME).toString()); entity.setGencadate(DateUtil.getSysDate()); List<TAcCauserapply> tAcCauserapplies = camgrManager.findCaUserApplayByNo(entity.getApplyno()); for (TAcCauserapply tAcCauserapply : tAcCauserapplies) { String canum = tAcCauserapply.getCanum(); if (NumberUtils.isNumber(canum)) { Number ncanum = NumberUtils.createNumber(canum); for (int i = 0; i < ncanum.intValue(); i++) { CAUserLicenseModel caUserLicenseModel = new CAUserLicenseModel(); caUserLicenseModel.setLoginname(tAcCauserapply.getLoginname()); caUserLicenseModels.add(caUserLicenseModel); } } } return "license"; }
From source file:com.intuit.tank.harness.functions.JexlNumericFunctionsTest.java
@Test(groups = TestGroups.FUNCTIONAL) public void testRandom() { String result = variables.evaluate("#{numericFunctions.random(two_int, four_float)}"); Assert.assertNotNull(result);//from w w w. j av a 2 s . c o m Assert.assertTrue(NumberUtils.createNumber(result).doubleValue() < 4D && NumberUtils.createNumber(result).doubleValue() >= 2D); result = variables.evaluate("#{numericFunctions.random(four_float)}"); Assert.assertNotNull(result); Assert.assertTrue(NumberUtils.createNumber(result).doubleValue() < 4D && NumberUtils.createNumber(result).doubleValue() >= 0D); }
From source file:com.intuit.tank.harness.functions.FunctionHandler.java
public static final Number getNumber(Object o) { Number ret = null;// ww w .j a v a 2 s. co m if (o instanceof Number) { ret = ((Number) o); } else { ret = NumberUtils.createNumber(o.toString()); } return ret; }
From source file:net.sf.json.util.JSONTokener.java
/** * Get the next value. The value can be a Boolean, Double, Integer, * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object. * * @throws JSONException If syntax error. * @return An object./*ww w . j a v a2 s . c om*/ */ public Object nextValue(JsonConfig jsonConfig) { char c = nextClean(); String s; switch (c) { case '"': case '\'': return nextString(c); case '{': back(); return JSONObject.fromObject(this, jsonConfig); case '[': back(); return JSONArray.fromObject(this, jsonConfig); default: // empty } /* * Handle unquoted text. This could be the values true, false, or null, or * it can be a number. An implementation (such as this one) is allowed to * also accept non-standard forms. Accumulate characters until we reach * the end of the text or a formatting character. */ StringBuffer sb = new StringBuffer(); char b = c; while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) { sb.append(c); c = next(); } back(); /* * If it is true, false, or null, return the proper value. */ s = sb.toString().trim(); if (s.equals("")) { throw syntaxError("Missing value."); } if (s.equalsIgnoreCase("true")) { return Boolean.TRUE; } if (s.equalsIgnoreCase("false")) { return Boolean.FALSE; } if (s.equals("null") || (jsonConfig.isJavascriptCompliant() && s.equals("undefined"))) { return JSONNull.getInstance(); } /* * If it might be a number, try converting it. We support the 0- and 0x- * conventions. If a number cannot be produced, then the value will just * be a string. Note that the 0-, 0x-, plus, and implied string * conventions are non-standard. A JSON parser is free to accept non-JSON * forms as long as it accepts all correct JSON forms. */ if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') { if (b == '0') { if (s.length() > 2 && (s.charAt(1) == 'x' || s.charAt(1) == 'X')) { try { return new Integer(Integer.parseInt(s.substring(2), 16)); } catch (Exception e) { /* Ignore the error */ } } else { try { return new Integer(Integer.parseInt(s, 8)); } catch (Exception e) { /* Ignore the error */ } } } try { return NumberUtils.createNumber(s); } catch (Exception e) { return s; } } if (JSONUtils.isFunctionHeader(s) || JSONUtils.isFunction(s)) { return s; } switch (peek()) { case ',': case '}': case '{': case '[': case ']': throw new JSONException("Unquotted string '" + s + "'"); } return s; }
From source file:com.jsmartframework.web.manager.ExpressionHandler.java
private void setExpressionValues(String expr, String jParam) { if (isReadOnlyParameter(jParam)) { return;/*from w w w. ja v a 2s. com*/ } Matcher matcher = EL_PATTERN.matcher(expr); if (matcher.find()) { String beanMethod = matcher.group(1); String[] methodSign = beanMethod.split(Constants.EL_SEPARATOR); if (methodSign.length > 0 && WebContext.containsAttribute(methodSign[0])) { beanMethod = String.format(Constants.JSP_EL, beanMethod); List<Object> list = new ArrayList<Object>(); String[] values = WebContext.getRequest().getParameterValues(TagHandler.J_ARRAY + jParam); boolean unescape = HANDLER.containsUnescapeMethod(methodSign); if (values != null) { for (String val : values) { try { list.add(NumberUtils.createNumber(val)); } catch (NumberFormatException e) { list.add(unescape ? val : escapeValue(val)); } } } // Check for empty value sent on array [false] if (list.size() == 1 && list.get(0) != null && list.get(0).equals("false")) { list.clear(); } ELContext context = WebContext.getPageContext().getELContext(); ValueExpression valueExpr = WebContext.getExpressionFactory().createValueExpression(context, beanMethod, Object.class); valueExpr.setValue(context, list); } } }
From source file:com.fengduo.bee.commons.core.lang.CollectionUtils.java
public static List<Number> getNumberProperty(Collection<?> values, String property) { if (values == null || values.isEmpty()) { return Collections.<Number>emptyList(); }//from w w w . j a v a 2 s . co m List<Number> valueResult = new ArrayList<Number>(values.size()); for (Object value : values) { try { String propertyValue = BeanUtils.getProperty(value, property); Number createNumber = NumberUtils.createNumber(propertyValue); valueResult.add(createNumber); } catch (Exception e) { logger.error(e.getMessage(), e); continue; } } return valueResult; }
From source file:net.ymate.platform.validation.validate.NumericValidator.java
public ValidateResult validate(ValidateContext context) { Object _paramValue = context.getParamValue(); if (_paramValue != null) { boolean _matched = false; boolean _flag = false; VNumeric _vNumeric = (VNumeric) context.getAnnotation(); try {//from ww w . j av a 2s . c om Number _number = NumberUtils.createNumber(BlurObject.bind(_paramValue).toStringValue()); if (_number == null) { _matched = true; _flag = true; } else { if (_vNumeric.min() > 0 && _number.doubleValue() < _vNumeric.min()) { _matched = true; } else if (_vNumeric.max() > 0 && _number.doubleValue() > _vNumeric.max()) { _matched = true; } } } catch (Exception e) { _matched = true; _flag = true; } if (_matched) { String _pName = StringUtils.defaultIfBlank(context.getParamLabel(), context.getParamName()); _pName = I18N.formatMessage(VALIDATION_I18N_RESOURCE, _pName, _pName); String _msg = StringUtils.trimToNull(_vNumeric.msg()); if (_msg != null) { _msg = I18N.formatMessage(VALIDATION_I18N_RESOURCE, _msg, _msg, _pName); } else { if (_flag) { String __NUMERIC = "ymp.validation.numeric"; _msg = I18N.formatMessage(VALIDATION_I18N_RESOURCE, __NUMERIC, "{0} not a valid numeric.", _pName); } else { if (_vNumeric.max() > 0 && _vNumeric.min() > 0) { String __NUMERIC_BETWEEN = "ymp.validation.numeric_between"; _msg = I18N.formatMessage(VALIDATION_I18N_RESOURCE, __NUMERIC_BETWEEN, "{0} numeric must be between {1} and {2}.", _pName, _vNumeric.min(), _vNumeric.max()); } else if (_vNumeric.max() > 0) { String __NUMERIC_MAX = "ymp.validation.numeric_max"; _msg = I18N.formatMessage(VALIDATION_I18N_RESOURCE, __NUMERIC_MAX, "{0} numeric must be lt {1}.", _pName, _vNumeric.max()); } else { String __NUMERIC_MIN = "ymp.validation.numeric_min"; _msg = I18N.formatMessage(VALIDATION_I18N_RESOURCE, __NUMERIC_MIN, "{0} numeric must be gt {1}.", _pName, _vNumeric.min()); } } } return new ValidateResult(context.getParamName(), _msg); } } return null; }
From source file:org.apache.accumulo.examples.wikisearch.function.QueryFunctions.java
public static Number abs(String fieldValue) { Number retval = null;//from w ww.j a v a 2 s . co m try { Number value = NumberUtils.createNumber(fieldValue); if (null == value) retval = (Number) Integer.MIN_VALUE; else if (value instanceof Long) retval = Math.abs(value.longValue()); else if (value instanceof Double) retval = Math.abs(value.doubleValue()); else if (value instanceof Float) retval = Math.abs(value.floatValue()); else if (value instanceof Integer) retval = Math.abs(value.intValue()); } catch (NumberFormatException nfe) { return (Number) Integer.MIN_VALUE; } return retval; }