Java BigDecimal Round roundDoubleAsBigDecimal(double d, int numberOfDecimals)

Here you can find the source of roundDoubleAsBigDecimal(double d, int numberOfDecimals)

Description

Rounds a double with the given number of decimals and returns a BigDecimal.

License

Apache License

Parameter

Parameter Description
d the double
numberOfDecimals the number of decimals

Return

the rounded BigDecimal

Declaration

public static BigDecimal roundDoubleAsBigDecimal(double d, int numberOfDecimals) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;

public class Main {
    /**//from   w  w  w .j a va 2s. co m
     * Rounds a double with the given number of decimals and returns a
     * BigDecimal.
     *
     * @param d the double
     * @param numberOfDecimals the number of decimals
     * @return the rounded BigDecimal
     */
    public static BigDecimal roundDoubleAsBigDecimal(double d, int numberOfDecimals) {
        BigDecimal bigDecimal = new BigDecimal(d).setScale(numberOfDecimals, BigDecimal.ROUND_HALF_UP);
        return bigDecimal;
    }
}

Related

  1. roundBigDecimal(BigDecimal num)
  2. roundBigDecimal(Number value, double precision)
  3. roundCommercial(BigDecimal value, int decimalPlaces)
  4. roundDecimal(Object value)
  5. roundDecimals(Float d)
  6. roundDown(BigDecimal a)
  7. rounded(BigDecimal amount)
  8. roundHalfUp2Scale(BigDecimal value)
  9. roundImpl(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode mode)