Java tutorial
//package com.java2s; import java.text.DecimalFormat; public class Main { private static final long canonicalDoubleNaN = Double.doubleToRawLongBits(Double.NaN); private static final long maxDouble = Double.doubleToLongBits(Double.MAX_VALUE); private static final long piDouble = Double.doubleToLongBits(Math.PI); private static final long eDouble = Double.doubleToLongBits(Math.E); private static final DecimalFormat format = new DecimalFormat("0.####################E0"); public static boolean isLikelyDouble(long value) { // Check for some common named double values // We don't check for Double.MIN_VALUE, which has a long representation of 1 if (value == canonicalDoubleNaN || value == maxDouble || value == piDouble || value == eDouble) { return true; } // Check for some named long values if (value == Long.MAX_VALUE || value == Long.MIN_VALUE) { return false; } // a non-canocical NaN is more likely to be an long double doubleValue = Double.longBitsToDouble(value); if (Double.isNaN(doubleValue)) { return false; } // Otherwise, whichever has a shorter scientific notation representation is more likely. // Long wins the tie String asLong = format.format(value); String asDouble = format.format(doubleValue); // try to strip off any small imprecision near the end of the mantissa int decimalPoint = asDouble.indexOf('.'); int exponent = asDouble.indexOf("E"); int zeros = asDouble.indexOf("000"); if (zeros > decimalPoint && zeros < exponent) { asDouble = asDouble.substring(0, zeros) + asDouble.substring(exponent); } else { int nines = asDouble.indexOf("999"); if (nines > decimalPoint && nines < exponent) { asDouble = asDouble.substring(0, nines) + asDouble.substring(exponent); } } return asDouble.length() < asLong.length(); } }