Example usage for java.math BigDecimal round

List of usage examples for java.math BigDecimal round

Introduction

In this page you can find the example usage for java.math BigDecimal round.

Prototype

public BigDecimal round(MathContext mc) 

Source Link

Document

Returns a BigDecimal rounded according to the MathContext settings.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("5.46497");

    MathContext mc = new MathContext(3); // 3 precision

    // bg1 is rounded using mc
    BigDecimal bg2 = bg1.round(mc);

    String str = "The value " + bg1 + " after rounding is " + bg2;

    // print bg2 value
    System.out.println(str);/*from ww  w . j  a v a 2s .c o  m*/
}

From source file:org.polymap.kaps.ui.form.NHK2010BewertungFormEditorPage.java

public static void main(String[] args) {
    System.out.println(Math.floor(9.5d + 0.5d));
    System.out.println(Math.floor(9.4d + 0.55d));
    System.out.println(Math.floor(9.49d + 0.55d));
    System.out.println(Math.floor(9.8d + 0.5d));
    System.out.println(Math.round(9.5d));
    System.out.println(Math.round(9.4d));
    System.out.println(Math.round((9.49d * 100) / 100));
    System.out.println(Math.round(9.8d));
    System.out.println(new DecimalFormat("###").format(9.49d));
    BigDecimal bd = new BigDecimal(9.49d);
    bd = bd.round(MathContext.UNLIMITED);
    System.out.println(bd.toString());

}

From source file:Main.java

/**
 * Returns a string representation of number rounded to given number of
 * significant figures//w  ww . j a  va2s . co m
 *
 * @param value
 * @param significantFigures
 * @return String.
 */
public static String roundToString(double value, int significantFigures) {
    MathContext mc = new MathContext(significantFigures);
    BigDecimal num = new BigDecimal(value);
    return num.round(mc).toPlainString();
}

From source file:net.vexelon.bgrates.Utils.java

public static String roundNumber(BigDecimal number, int n) {
    return number.round(new MathContext(n, RoundingMode.HALF_UP)).toPlainString();
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    //  and then add one again to get the next number
    //  so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    //special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }//from   w  w w  .j a va 2 s  . com

    StringBuilder sb = new StringBuilder();
    //number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    //so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    //this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    //Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    // and then add one again to get the next number
    // so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    // special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }/*from www  .j ava  2s.c  o m*/

    StringBuilder sb = new StringBuilder();
    // number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    // so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    // this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    // Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}

From source file:org.apache.phoenix.util.TestUtil.java

public static void assertRoundEquals(BigDecimal bd1, BigDecimal bd2) {
    bd1 = bd1.round(PDataType.DEFAULT_MATH_CONTEXT);
    bd2 = bd2.round(PDataType.DEFAULT_MATH_CONTEXT);
    if (bd1.compareTo(bd2) != 0) {
        fail("expected:<" + bd1 + "> but was:<" + bd2 + ">");
    }/*from w w w  . ja v a  2s  .  co m*/
}

From source file:gdsc.smlm.ij.utils.Utils.java

/**
 * Round the double to the specified significant digits
 * /* w  ww.jav  a 2s.co  m*/
 * @param d
 * @param significantDigits
 * @return
 */
public static String rounded(double d, int significantDigits) {
    if (Double.isInfinite(d) || Double.isNaN(d))
        return "" + d;
    BigDecimal bd = new BigDecimal(d);
    bd = bd.round(new MathContext(significantDigits));
    return "" + bd.doubleValue();
}

From source file:au.org.ala.delta.model.attribute.SignificantFiguresAttributeChunkFormatter.java

/**
 * Overrides formatNumber in the parent class to format the number to 5 significant figures.  Trailing
 * zeros are stripped./*from   w  w w  . j a v  a 2s  .  c  om*/
 * Note: for compatibility with the original CONFOR significant figures are only applied to values after
 * the decimal place. (e.g. 123456.7 will be formatted as 123456, not 123460)
 * @param number the number to format.
 * @return the supplied number as a String.
 */
@Override
public String formatNumber(BigDecimal number) {

    int significantFigures = determinePrecision(number);
    MathContext context = new MathContext(significantFigures, RoundingMode.HALF_UP);

    BigDecimal result = number.round(context);
    result = result.stripTrailingZeros();
    return result.toPlainString();
}

From source file:com.ibk.ltw.domain.Product.java

/**
 *
 * @return Price with VAT//from  w  w  w  .  j  a  va  2s. co  m
 */
public int getGrossPriceInCents() {
    BigDecimal grossPriceInCents = new BigDecimal(netPriceInCents)
            .multiply(new BigDecimal(1000 + vatService.getVatInPerThousand())).movePointLeft(3);
    return grossPriceInCents.round(new MathContext(0, RoundingMode.HALF_UP)).intValue();
}