Java Double to Long doubleToSortableLong(double val)

Here you can find the source of doubleToSortableLong(double val)

Description

Converts a double value to a sortable signed long.

License

Apache License

Declaration

public static long doubleToSortableLong(double val) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**/*from   w w  w.ja  va 2s. c o m*/
     * Converts a <code>double</code> value to a sortable signed <code>long</code>.
     * The value is converted by getting their IEEE 754 floating-point &quot;double format&quot;
     * bit layout and then some bits are swapped, to be able to compare the result as long.
     * By this the precision is not reduced, but the value can easily used as a long.
     * The sort order (including {@link Double#NaN}) is defined by
     * {@link Double#compareTo}; {@code NaN} is greater than positive infinity.
     * @see #sortableLongToDouble
     */
    public static long doubleToSortableLong(double val) {
        return sortableDoubleBits(Double.doubleToLongBits(val));
    }

    /** Converts IEEE 754 representation of a double to sortable order (or back to the original) */
    public static long sortableDoubleBits(long bits) {
        return bits ^ (bits >> 63) & 0x7fffffffffffffffL;
    }
}

Related

  1. doubleToLongBits(final double d)
  2. doubleToLongBits(final double v)
  3. doubleToRawLongBits(double d)
  4. doubleToRawLongBits(double value)
  5. doubleToS390LongBits(double ieeeDouble)
  6. doubleToSortableLong(double value)