List of usage examples for org.apache.commons.lang3.math NumberUtils toDouble
public static double toDouble(final String str, final double defaultValue)
Convert a String
to a double
, returning a default value if the conversion fails.
If the string str
is null
, the default value is returned.
NumberUtils.toDouble(null, 1.1d) = 1.1d NumberUtils.toDouble("", 1.1d) = 1.1d NumberUtils.toDouble("1.5", 0.0d) = 1.5d
From source file:io.wcm.config.core.management.util.TypeConversion.java
/** * Converts a string value to an object with the given parameter type. * @param value String value/* www. ja va2 s . c o m*/ * @param type Parameter type * @return Converted value * @throws IllegalArgumentException If type is not supported */ @SuppressWarnings("unchecked") public static <T> T stringToObject(String value, Class<T> type) { if (value == null) { return null; } if (type == String.class) { return (T) value; } else if (type == String[].class) { return (T) StringUtils.splitPreserveAllTokens(value, ARRAY_DELIMITER); } if (type == Integer.class) { return (T) (Integer) NumberUtils.toInt(value, 0); } if (type == Long.class) { return (T) (Long) NumberUtils.toLong(value, 0L); } if (type == Double.class) { return (T) (Double) NumberUtils.toDouble(value, 0d); } if (type == Boolean.class) { return (T) (Boolean) BooleanUtils.toBoolean(value); } if (type == Map.class) { String[] rows = StringUtils.splitPreserveAllTokens(value, ARRAY_DELIMITER); Map<String, String> map = new LinkedHashMap<>(); for (int i = 0; i < rows.length; i++) { String[] keyValue = StringUtils.splitPreserveAllTokens(rows[i], KEY_VALUE_DELIMITER); if (keyValue.length == 2 && StringUtils.isNotEmpty(keyValue[0])) { map.put(keyValue[0], StringUtils.isEmpty(keyValue[1]) ? null : keyValue[1]); } } return (T) map; } throw new IllegalArgumentException("Unsupported type: " + type.getName()); }
From source file:info.donsun.core.utils.Values.java
/** * ????//w w w . java 2s. c o m * * @param obj * @param defaultValue null * @return ?? */ public static double getDouble(Object obj, double defaultValue) { return NumberUtils.toDouble(getString(obj), defaultValue); }
From source file:com.jkoolcloud.tnt4j.streams.utils.DoubleRange.java
/** * Makes range object using values parsed from {@code rangeStr}. * <p>/* ww w. j a va2 s .c om*/ * If {@code rangeStr} has missing range bound values, default ones are set: lower * {@code positive ? 0 : -Double.MAX_VALUE}, upper {@code Double.MAX_VALUE}. * <p> * Range separator symbol is '{@value com.jkoolcloud.tnt4j.streams.utils.Range#RANGE_SEPARATOR}'. * * @param rangeStr * range definition string to parse * @param positive * {@code true} means range has only positive values, {@code} - range can have negative values. * @return double range parsed from range definition string * @throws Exception * if range string can't be parsed * @see Range#parseRange(String, Pattern) */ public static DoubleRange getRange(String rangeStr, boolean positive) throws Exception { String[] rangeTokens = parseRange(rangeStr, positive ? D_PATTERN_POSITIVE : D_PATTERN); double from = NumberUtils.toDouble(rangeTokens[0], positive ? 0 : -Double.MAX_VALUE); double to = NumberUtils.toDouble(rangeTokens[1], Double.MAX_VALUE); return new DoubleRange(from, to); }
From source file:com.erudika.para.i18n.OXRCurrencyConverter.java
@Override public Double convertCurrency(Number amount, String from, String to) { if (amount == null || StringUtils.isBlank(from) || StringUtils.isBlank(to)) { return 0.0; }/*from www.j av a2s . com*/ Sysprop s = dao.read(FXRATES_KEY); if (s == null) { s = fetchFxRatesJSON(); } else if ((Utils.timestamp() - s.getTimestamp()) > REFRESH_AFTER) { // lazy refresh fx rates Para.asyncExecute(new Runnable() { public void run() { fetchFxRatesJSON(); } }); } double ratio = 1.0; if (s.hasProperty(from) && s.hasProperty(to)) { Double f = NumberUtils.toDouble(s.getProperty(from).toString(), 1.0); Double t = NumberUtils.toDouble(s.getProperty(to).toString(), 1.0); ratio = t / f; } return amount.doubleValue() * ratio; }
From source file:net.gtaun.shoebill.util.config.AbstractConfiguration.java
@Override public double getDouble(String path, double def) { Object obj = get(path);/* www. j a v a2s . c om*/ if (obj instanceof Double) return (Double) obj; return NumberUtils.toDouble(obj.toString(), def); }
From source file:com.tealcube.minecraft.bukkit.config.MasterConfiguration.java
public double getDouble(String key, double fallback) { if (settingMap == null || !settingMap.containsKey(key)) { return fallback; }/*www . j a v a 2s .c o m*/ Object val = settingMap.get(key); if (val instanceof Double) { return (Double) val; } if (val instanceof String) { return NumberUtils.toDouble((String) val, fallback); } return fallback; }
From source file:net.gtaun.shoebill.util.config.AbstractConfiguration.java
@Override public List<Double> getDoubleList(String path, List<Double> def) { List<?> raw = getList(path); if (raw == null) return (def != null) ? def : new ArrayList<>(); return raw.stream().map(o -> NumberUtils.toDouble(o.toString(), 0.0)).collect(Collectors.toList()); }
From source file:com.mirth.connect.connectors.doc.DocumentWriter.java
@Override public boolean checkProperties(ConnectorProperties properties, boolean highlight) { DocumentDispatcherProperties props = (DocumentDispatcherProperties) properties; boolean valid = true; if (!outputAttachmentRadioButton.isSelected() && props.getHost().length() == 0) { valid = false;/*from ww w . j a v a 2 s . co m*/ if (highlight) { directoryField.setBackground(UIConstants.INVALID_COLOR); } } if (!outputAttachmentRadioButton.isSelected() && props.getOutputPattern().length() == 0) { valid = false; if (highlight) { fileNameField.setBackground(UIConstants.INVALID_COLOR); } } if (props.getTemplate().length() == 0) { valid = false; if (highlight) { templateTextArea.setBackground(UIConstants.INVALID_COLOR); } } if (props.isEncrypt()) { if (props.getPassword().length() == 0) { valid = false; if (highlight) { passwordField.setBackground(UIConstants.INVALID_COLOR); } } } if (StringUtils.isBlank(props.getPageWidth()) || NumberUtils.toDouble(props.getPageWidth(), 1) <= 0) { valid = false; if (highlight) { pageSizeWidthField.setBackground(UIConstants.INVALID_COLOR); } } if (StringUtils.isBlank(props.getPageHeight()) || NumberUtils.toDouble(props.getPageHeight(), 1) <= 0) { valid = false; if (highlight) { pageSizeHeightField.setBackground(UIConstants.INVALID_COLOR); } } return valid; }
From source file:io.wcm.sling.commons.request.RequestParam.java
/** * Returns a request parameter as double. * @param request Request.// ww w.j ava 2 s . c om * @param param Parameter name. * @param defaultValue Default value. * @return Parameter value or default value if it does not exist or is not a number. */ public static double getDouble(ServletRequest request, String param, double defaultValue) { String value = request.getParameter(param); return NumberUtils.toDouble(value, defaultValue); }
From source file:ezbake.common.properties.EzProperties.java
/** * Get a property as a double, if the property doesn't exist or can't be converted then we return the default value * * @param propertyName is the name of the property we are looking for (the key) * @param defaultValue is the value to return if the key doesn't exist or the value can't be converted to a double * * @return either the properly parsed double or the default value if the key doesn't exist or can't be converted */// ww w .j av a2s .c o m public double getDouble(String propertyName, double defaultValue) { return NumberUtils.toDouble(getProperty(propertyName), defaultValue); }