Java Decimal Format isLikelyDouble(long value)

Here you can find the source of isLikelyDouble(long value)

Description

is Likely Double

License

Open Source License

Declaration

public static boolean isLikelyDouble(long value) 

Method Source Code

//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;
        }//from   w ww . j  av  a  2s .c  o m

        // 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();
    }
}

Related

  1. getStringFromDouble(double number)
  2. getStringRepresentationForDouble(double value)
  3. getTwoPoint(double val)
  4. getValue(double value)
  5. isDouble(String _str)
  6. isprime(double x)
  7. isValidDouble(String value, int decimals)
  8. Julian_Cal(double JDN)
  9. listToString(List list)