Java Decimal Round roundDouble(Double val, int precision)

Here you can find the source of roundDouble(Double val, int precision)

Description

round Double

License

Open Source License

Declaration

public static double roundDouble(Double val, int precision) 

Method Source Code

//package com.java2s;
/*/*from   w w  w .j  av  a 2  s. c o m*/
 * Copyright (c) 2013. Chandra Tungaturthi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.math.BigDecimal;
import java.math.MathContext;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.*;

import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
    private static final Logger mylogger = Logger.getLogger("com.lia.core");

    public static double roundDouble(Double val, int precision) {

        Double dVal = adjDecimalSep(val);

        BigDecimal bigNumber1 = new BigDecimal(dVal, MathContext.UNLIMITED); // dec 64 - > double precision
        return bigNumber1.setScale(precision, BigDecimal.ROUND_CEILING).doubleValue();

    }

    public static double adjDecimalSep(double value) {
        mylogger.log(Level.FINE, "Adjusting valu {0}", new Object[] { value });
        // Bug fix: 
        //        For german locale 3.333 => 3,33 which would raise an error 
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
        otherSymbols.setDecimalSeparator('.');
        otherSymbols.setGroupingSeparator(',');
        // Bug fix
        DecimalFormat newFormat = new DecimalFormat();
        newFormat.setDecimalFormatSymbols(otherSymbols);
        return Double.valueOf(newFormat.format(value));

    }
}

Related

  1. roundDecimals(double d, int numOfDec)
  2. roundDistanceFromMeterToKm(final double distanceInMeter)
  3. roundDouble(double iVal)
  4. roundDouble(double num, int decimal)
  5. roundDouble(double numero, int ceros_a_la_derecha)
  6. roundedNumber(double inVal)
  7. RoundFractionalTons(double d)
  8. roundNumDecimals(double d, int num)
  9. roundOffToTwoDecimal(double value)