Java BigDecimal Round roundBigDecimal(Number value, double precision)

Here you can find the source of roundBigDecimal(Number value, double precision)

Description

rounding with big decimal precision

License

Open Source License

Declaration

public static Number roundBigDecimal(Number value, double precision) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://  w  w w.ja  v  a  2 s  . c o m
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.math.BigDecimal;

public class Main {
    /**
     * rounding with big decimal precision
     */
    public static Number roundBigDecimal(Number value, double precision) {
        if (value != null) {
            BigDecimal val = value instanceof BigDecimal ? (BigDecimal) value : new BigDecimal(value.toString());
            BigDecimal prec = BigDecimal.valueOf(precision);
            return val.divide(prec, BigDecimal.ROUND_HALF_EVEN).setScale(0, BigDecimal.ROUND_HALF_EVEN)
                    .multiply(prec);
        }
        return null;
    }
}

Related

  1. round(BigDecimal what, int howmuch)
  2. round(final BigDecimal input, final int precision)
  3. round(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode roundingMode)
  4. round_BigDecimal(double d, int decLen)
  5. roundBigDecimal(BigDecimal num)
  6. roundCommercial(BigDecimal value, int decimalPlaces)
  7. roundDecimal(Object value)
  8. roundDecimals(Float d)
  9. roundDoubleAsBigDecimal(double d, int numberOfDecimals)