Java Number Round roundValues(Double value1, Double value2)

Here you can find the source of roundValues(Double value1, Double value2)

Description

Rounds to 2 significant figures, relatively.

License

Open Source License

Declaration

public static double[] roundValues(Double value1, Double value2) 

Method Source Code

//package com.java2s;
/*/*w ww  .  j a  va 2s.  c om*/
 * Boltzmann 3D, a kinetic theory demonstrator
 * Copyright (C) 2013 Dr. Randall B. Shirts
 * 
 * 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 {
    /**
     * Rounds to 2 significant figures, relatively. The larger magnitude
     * value determines where these significant figures lie.
     */
    public static double[] roundValues(Double value1, Double value2) {
        if (value1.isInfinite() || value1.isNaN() || value1 == 0 || value2.isInfinite() || value2.isNaN()
                || value2 == 0)
            return new double[] { roundValue(value1), roundValue(value2) };

        double val1 = value1;
        double val2 = value2;
        double biggerValue;

        //Significant figures are determined by the larger magnitude value.
        if (Math.abs(val1) >= Math.abs(val2))
            biggerValue = val1;
        else
            biggerValue = val2;

        int shift = 0;
        if (Math.abs(biggerValue) >= 1) // |biggerValue| >= 1
        {
            //Make sure we're starting with a value >= 10 (2 digits).
            val1 *= 10;
            val2 *= 10;
            biggerValue *= 10;

            //Consider the two leading numbers.
            while (Math.abs(biggerValue) > 100) {
                val1 /= 10.0;
                val2 /= 10.0;
                biggerValue /= 10.0;
                shift++;
            }

            //Set all lesser numbers to zero.
            val1 = Math.round(val1);
            val2 = Math.round(val2);

            //Restore original magnitude.
            for (; shift > 0; shift--) {
                val1 *= 10;
                val2 *= 10;
            }

            //One last divide to offset the first multiplication.
            val1 /= 10;
            val2 /= 10;
        } else // 0 < |biggerValue| < 1
        {
            //Consider the two leading numbers.
            while (Math.abs(biggerValue) < 10) {
                val1 *= 10;
                val2 *= 10;
                biggerValue *= 10;
                shift++;
            }

            //Set all lesser numbers to zero.
            val1 = Math.round(val1);
            val2 = Math.round(val2);

            //Restore original magnitude.
            for (; shift > 0; shift--) {
                val1 /= 10.0;
                val2 /= 10.0;
            }
        }

        return new double[] { val1, val2 };
    }

    /** Rounds to 2 significant figures. */
    protected static double roundValue(Double value) {
        if (value.isInfinite() || value.isNaN() || value == 0)
            return value;

        double val = value;
        int shift = 0;
        if (Math.abs(val) >= 1) // |val| >= 1
        {
            //Make sure we're starting with a value >= 10 (2 digits).
            val *= 10;

            //Consider the two leading numbers.
            while (Math.abs(val) > 100) {
                val /= 10.0;
                shift++;
            }

            //Set all lesser numbers to zero.
            val = Math.round(val);

            //Restore original magnitude.
            for (; shift > 0; shift--)
                val *= 10;

            //One last divide to offset the first multiplication.
            val /= 10;
        } else // 0 < |val| < 1
        {
            //Consider the two leading numbers.
            while (Math.abs(val) < 10) {
                val *= 10;
                shift++;
            }

            //Set all lesser numbers to zero.
            val = Math.round(val);

            //Restore original magnitude.
            for (; shift > 0; shift--)
                val /= 10.0;
        }

        return val;
    }
}

Related

  1. roundUpToPowerOfTwo(int x)
  2. roundUpToTheNextHighestPowerOf2(int v)
  3. roundUpToTwo(Double number)
  4. roundValue(Double value)
  5. roundValue(final double value)
  6. roundValueToUnit(final double graphValue, final double graphUnit, final boolean isMinValue)
  7. roundy(double a)
  8. roundy(double a, double precision)