public static boolean isNumeric(String str) { try { double d = Double.parseDouble(str); } catch(NumberFormatException nfe) { return false; } return true; }
@Override protected Double doForward(String value) { return Double.valueOf(value); }
@Override protected String doBackward(Double value) { return value.toString(); }
private static String doubleToString(double value) { if (Double.compare(value, Math.floor(value)) == 0 && !Double.isInfinite(value)) { return Integer.toString((int) value); } // remove trailing zeroes NumberFormat f = NumberFormat.getInstance(Locale.ROOT); f.setMaximumFractionDigits(4); f.setMinimumIntegerDigits(1); return f.format(value); }
/** * Converts a "double" value between endian systems. * @param value value to convert * @return the converted value */ public static double swapDouble(final double value) { return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) ); }
@Override public void writeTag(Object data, MBOut out) { double d = ((Double) data).doubleValue(); { byte[] bytes = Double.toString(d).getBytes(); out.writeArray(bytes, 0, bytes.length); } }
@Override public Object decode(Object jv) { if (jv instanceof Number) { return new Double(((Number) jv).doubleValue()); } return (Double) null; } };
public static double unboxed(Double v) { return v == null ? 0 : v.doubleValue(); }
/** Returns whether any of the values in {@code dataset} are {@code NaN}. */ private static boolean containsNaN(double... dataset) { for (double value : dataset) { if (Double.isNaN(value)) { return true; } } return false; }
private static boolean areEqual(double a, double b) { return Double.doubleToLongBits(a) == Double.doubleToLongBits(b); }
public void testIsFinite() { for (double value : NUMBERS) { assertEquals(!(Double.isNaN(value) || Double.isInfinite(value)), Doubles.isFinite(value)); } }
/** * Compares the two specified {@code double} values. The sign of the value returned is the same as * that of <code>((Double) a).{@linkplain Double#compareTo compareTo}(b)</code>. As with that * method, {@code NaN} is treated as greater than all other values, and {@code 0.0 > -0.0}. * * <p><b>Note:</b> this method simply delegates to the JDK method {@link Double#compare}. It is * provided for consistency with the other primitive types, whose compare methods were not added * to the JDK until JDK 7. * * @param a the first {@code double} to compare * @param b the second {@code double} to compare * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is * greater than {@code b}; or zero if they are equal */ public static int compare(double a, double b) { return Double.compare(a, b); }
@Override public Object decode(Object jv) { if (jv instanceof Number) { return new Double(((Number) jv).doubleValue()); } return (Double) null; } };
public static double unboxed(Double v) { return v == null ? 0 : v.doubleValue(); }
// 1. static method if (Double.isNaN(doubleValue)) { ... } // 2. object's method if (doubleObject.isNan()) { ... }
public static long doubleToLongBits (double value) { return Double.doubleToLongBits(value); }
@GwtIncompatible // DoubleMath.isPowerOfTwo, DoubleMath.log2(double, RoundingMode), StrictMath public void testIsPowerOfTwo() { for (double x : ALL_DOUBLE_CANDIDATES) { boolean expected = x > 0 && !Double.isInfinite(x) && !Double.isNaN(x) && StrictMath.pow(2.0, DoubleMath.log2(x, FLOOR)) == x; assertEquals(expected, DoubleMath.isPowerOfTwo(x)); } }