Java Double to doubleToBits(double in, double min, double max, int numBits, int splits)

Here you can find the source of doubleToBits(double in, double min, double max, int numBits, int splits)

Description

Turns a double into a bit string (0s and 1s).

License

Open Source License

Parameter

Parameter Description
in the double value
min the minimum
max the maximum
numBits the number of bits
splits the number of splits

Return

the generated bit string

Declaration

public static String doubleToBits(double in, double min, double max, int numBits, int splits) 

Method Source Code

//package com.java2s;
/*// w ww  . j  a  v  a2  s  .com
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Turns a double into a bit string (0s and 1s).
     *
     * @param in      the double value
     * @param min      the minimum
     * @param max      the maximum
     * @param numBits   the number of bits
     * @param splits   the number of splits
     * @return      the generated bit string
     */
    public static String doubleToBits(double in, double min, double max, int numBits, int splits) {

        if (in > max) {
            in = max;
        }
        if (in < min) {
            in = min;
        }
        double sdist = (max - min) / ((double) splits - 1);
        double dist = in - min;
        double rat = dist / sdist;
        int split = (int) Math.round(rat);

        String bits = Integer.toBinaryString(split);
        while (bits.length() < numBits)
            bits = "0" + bits;

        return bits;
    }
}

Related

  1. doubleTo2DArray(double[] array)
  2. doubleToBasicType(double d, Class clazz)
  3. doubleToBinaryString(double value)
  4. doubleToBits(double num)
  5. doubleToChar(double d, char defaultValue)
  6. doubleToDoubleArray(double... a)
  7. doubleToFloatArray(double[] dArray)