Java Number Round roundValueToUnit(final double graphValue, final double graphUnit, final boolean isMinValue)

Here you can find the source of roundValueToUnit(final double graphValue, final double graphUnit, final boolean isMinValue)

Description

Round floating value by removing the trailing part, which causes problem when creating units.

License

Open Source License

Parameter

Parameter Description
graphValue a parameter
graphUnit a parameter

Declaration

public static double roundValueToUnit(final double graphValue, final double graphUnit,
        final boolean isMinValue) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2005, 2016 Wolfgang Schramm and Contributors
 *
 * 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 version 2 of the License.//  ww  w .  j  ava 2  s . c o m
 *
 * 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, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *******************************************************************************/

public class Main {
    /**
     * Round floating value by removing the trailing part, which causes problem when creating units.
     * For the value 200.00004 the .00004 part will be removed
     * 
     * @param graphValue
     * @param graphUnit
     * @return
     */
    public static double roundValueToUnit(final double graphValue, final double graphUnit,
            final boolean isMinValue) {

        if (graphUnit < 1) {

            if (graphValue < 0) {

                final double gvDiv1 = graphValue / graphUnit;
                final int gvDiv2 = (int) (gvDiv1 - 0.5f);
                final double gvDiv3 = gvDiv2 * graphUnit;

                return gvDiv3;

            } else {

                final double gvDiv1 = graphValue / graphUnit;
                final int gvDiv2 = (int) (gvDiv1 + (isMinValue ? -0.5f : 0.5f));
                final double gvDiv3 = gvDiv2 * graphUnit;

                return gvDiv3;
            }

        } else {

            // graphUnit >= 1

            if (graphValue < 0) {

                final double gvDiv1 = graphValue * graphUnit;
                final long gvDiv2 = (long) (gvDiv1 + (isMinValue ? -0.5f : 0.5f));
                final double gvDiv3 = gvDiv2 / graphUnit;

                return gvDiv3;

            } else {

                // graphValue >= 0

                final double gvDiv1 = graphValue * graphUnit;
                final long gvDiv2 = (long) (gvDiv1 + 0.5f);
                final double gvDiv3 = gvDiv2 / graphUnit;

                return gvDiv3;
            }
        }
    }
}

Related

  1. roundUpToTheNextHighestPowerOf2(int v)
  2. roundUpToTwo(Double number)
  3. roundValue(Double value)
  4. roundValue(final double value)
  5. roundValues(Double value1, Double value2)
  6. roundy(double a)
  7. roundy(double a, double precision)