Java Double Number Round round(double value, int decimal)

Here you can find the source of round(double value, int decimal)

Description

round

License

Apache License

Declaration

public static double round(double value, int decimal) 

Method Source Code

//package com.java2s;
/*//  ww w .  ja va 2 s  . c  om
 * Created on 01.02.2005
 *
 * JDBReport Generator
 * 
 * Copyright (C) 2005-2014 Andrey Kholmanskih
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

public class Main {
    public static double round(double value) {
        return round(value, 0);
    }

    public static double round(double value, int decimal) {
        long d = 1;
        int c = decimal < 0 ? -decimal : decimal;
        for (int i = 0; i < c; i++) {
            d = d * 10;
        }
        if (decimal >= 0) {
            return (double) Math.round(value * d) / d;
        }
        return Math.round(value / d) * d;
    }

    public static float round(float value, int decimal) {
        int d = 1;
        int c = decimal < 0 ? -decimal : decimal;
        for (int i = 0; i < c; i++) {
            d = d * 10;
        }
        if (decimal >= 0) {
            int v = Math.round(value * d);
            return 1.0f * v / d;
        }
        return Math.round(value / d) * d;
    }
}

Related

  1. round(double value, double decimalplaces)
  2. round(double value, double factor)
  3. round(double value, double increment)
  4. round(double value, double precision)
  5. round(double value, int decimal)
  6. round(double value, int decimalPlace)
  7. round(double value, int decimalPlaces)
  8. round(double value, int decimalPlaces)
  9. round(double value, int decimals)