Java Number Round roundUp(double val)

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

Description

round Up

License

GNU General Public License

Declaration

public static double roundUp(double val) 

Method Source Code

//package com.java2s;
/**/*w ww  .  j  av  a2  s  .  co  m*/
 *    Copyright (C)  DataMelt project. The jHPLot package.
 *    Includes coding developed for Centre d'Informatique Geologique
 *    by J.V.Lee priory 2000 GNU license.
 *
 *    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 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>.
 *
 *    Additional permission under GNU GPL version 3 section 7:
 *    If you have received this program as a library with written permission from the DataMelt team,
 *    you can link or combine this library with your non-GPL project to convey the resulting work.
 *    In this case, this library should be considered as released under the terms of
 *    GNU Lesser public license (see <https://www.gnu.org/licenses/lgpl.html>),
 *    provided you include this license notice and a URL through which recipients can access the
 *    Corresponding Source.
 **/

public class Main {
    public static double roundUp(double val) {
        int exponent = (int) Math.floor(Math.log10(val));
        val *= Math.pow(10, -exponent);
        if (val > 5.0)
            val = 10.0;
        else if (val > 2.0)
            val = 5.0;
        else if (val > 1.0)
            val = 2.0;
        val *= Math.pow(10, exponent);
        return val;
    }

    /**
     * Round up the passed value to a NICE value.
     */

    static public double RoundUp(double val) {
        int exponent;
        int i;

        exponent = (int) (Math.floor(Math.log10(val)));

        if (exponent < 0) {
            for (i = exponent; i < 0; i++) {
                val *= 10.0;
            }
        } else {
            for (i = 0; i < exponent; i++) {
                val /= 10.0;
            }
        }

        if (val > 5.0)
            val = 10.0;
        else if (val > 2.0)
            val = 5.0;
        else if (val > 1.0)
            val = 2.0;
        else
            val = 1.0;

        if (exponent < 0) {
            for (i = exponent; i < 0; i++) {
                val /= 10.0;
            }
        } else {
            for (i = 0; i < exponent; i++) {
                val *= 10.0;
            }
        }

        return val;

    }
}

Related

  1. roundUP(double d)
  2. roundUp(double d)
  3. roundUp(double n)
  4. roundUp(double n, int precision)
  5. roundUp(double val)
  6. roundUp(final double val)
  7. roundUp(final int number, final int base)
  8. RoundUp(final T number)
  9. roundUp(int capacity)