List of usage examples for java.lang Number doubleValue
public abstract double doubleValue();
From source file:net.sf.jasperreports.functions.standard.MathFunctions.java
private static Number fixNumberReturnType(Number returnValue, Number... numbers) { if (haveSameType(Integer.class, numbers)) return returnValue.intValue(); if (haveSameType(Long.class, numbers)) return returnValue.longValue(); if (haveSameType(Float.class, numbers)) return returnValue.floatValue(); return returnValue.doubleValue(); }
From source file:de.christianseipl.utilities.maputils.MapMath.java
/** * Fhrt die {@link Math#round(double)} Operation auf jedem Element der Map * durch// w w w . jav a 2 s .c om * * @param <K> ein beliebiger Objekttyp * * @param _map * Die Ursprungsmap * @return Eine neue Map, deren Wert das Ergebnis von * {@link Math#round(double)} darstellen. */ public static <K> Map<K, Double> round(Map<? extends K, ? extends Number> _map) { return operateOnMap(_map, new NumberTransformer<K, Double>() { @Override public Double transform(K _key, Number _number) { return Double.valueOf(Math.round(_number.doubleValue())); } }); }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a float primitive. * /*from w w w.j a va2 s.c o m*/ * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null) * @return the converted Float value. */ public static Float formatFloat(String value, Locale locale) { Float result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getInstance(locale); } else { formatter = NumberFormat.getInstance(Locale.getDefault()); } ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= (Float.MAX_VALUE * -1) && num.doubleValue() <= Float.MAX_VALUE) { result = new Float(num.floatValue()); } } } return result; }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a double primitive. * /*from w w w . j a v a 2 s .c o m*/ * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null) * @return the converted Double value. */ public static Double formatDouble(String value, Locale locale) { Double result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getInstance(locale); } else { formatter = NumberFormat.getInstance(Locale.getDefault()); } ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= (Double.MAX_VALUE * -1) && num.doubleValue() <= Double.MAX_VALUE) { result = new Double(num.doubleValue()); } } } return result; }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a byte primitive. * /* www. j a v a 2s . com*/ * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null) * @return the converted Byte value. */ public static Byte formatByte(String value, Locale locale) { Byte result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) { result = new Byte(num.byteValue()); } } } return result; }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a short primitive. * //w w w. j a v a 2 s . c om * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null)vv * @return the converted Short value. */ public static Short formatShort(String value, Locale locale) { Short result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Short.MIN_VALUE && num.doubleValue() <= Short.MAX_VALUE) { result = new Short(num.shortValue()); } } } return result; }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to an int primitive. * /*w w w. j a v a 2 s. c om*/ * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null) * @return the converted Integer value. */ public static Integer formatInt(String value, Locale locale) { Integer result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) { result = new Integer(num.intValue()); } } } return result; }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a long primitive. * // www .ja v a 2 s. co m * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null) * @return the converted Long value. */ public static Long formatLong(String value, Locale locale) { Long result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) { result = new Long(num.longValue()); } } } return result; }
From source file:de.christianseipl.utilities.maputils.MapMath.java
/** * Fhrt die {@link MathUtils#round(double, int)} Operation auf jedem Element der Map * durch. Die Anzahl an signifikanten Stellen kann hierbei vorgegeben werden. * //from w w w . j av a 2s .c o m * @param <K> ein beliebiger Objekttyp * * @param _map * Die Ursprungsmap * @param _scale Die Anzahl an signifikanten Stellen. * @return Eine neue Map, deren Wert das Ergebnis von * {@link Math#round(double)} darstellen. */ public static <K> Map<K, Double> round(Map<? extends K, ? extends Number> _map, final int _scale) { return operateOnMap(_map, new NumberTransformer<K, Double>() { @Override public Double transform(K _key, Number _number) { return Double.valueOf(MathUtils.round(_number.doubleValue(), _scale)); } }); }
From source file:com.ning.metrics.serialization.event.SmileEnvelopeEvent.java
private static void addToTree(ObjectNode root, String name, Object value) { /* could wrap everything as POJONode, but that's bit inefficient; * so let's handle some known cases. * (in reality, I doubt there could ever be non-scalars, FWIW, since * downstream systems expect simple key/value data) *///from w w w . j a va 2 s .co m if (value instanceof String) { root.put(name, (String) value); return; } if (value instanceof Number) { Number num = (Number) value; if (value instanceof Integer) { root.put(name, num.intValue()); } else if (value instanceof Long) { root.put(name, num.longValue()); } else if (value instanceof Double) { root.put(name, num.doubleValue()); } else { root.putPOJO(name, num); } } else if (value == Boolean.TRUE) { root.put(name, true); } else if (value == Boolean.FALSE) { root.put(name, false); } else { // most likely Date root.putPOJO(name, value); } }