Example usage for java.math MathContext MathContext

List of usage examples for java.math MathContext MathContext

Introduction

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

Prototype

public MathContext(String val) 

Source Link

Document

Constructs a new MathContext from a string.

Usage

From source file:Main.java

public static void main(String[] args) {

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

    // assign value to bg1
    BigDecimal bg1 = new BigDecimal("123.1234");

    // assign negate value of bg1 to bg2 using mc
    BigDecimal bg2 = bg1.negate(mc);

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

From source file:Main.java

public static void main(String[] args) {

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

    BigDecimal bg1 = new BigDecimal("100.123");
    BigDecimal bg2 = new BigDecimal("50.56");

    // subtract bg1 with bg2 using mc and assign result to bg3
    BigDecimal bg3 = bg1.subtract(bg2, mc);

    String str = "The Result of Subtraction is " + bg3;

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

From source file:Main.java

public static void main(String[] args) {

    MathContext mc = new MathContext(2);
    MathContext mc1 = new MathContext(4);

    BigDecimal bg1 = new BigDecimal("123.1234");

    // assign absolute value of bg1 to bg2 rounded to 2 precision using mc
    BigDecimal bg2 = bg1.abs(mc);

    // assign absolute value of bg1 to bg3 rounded to 4 precision using mc1
    BigDecimal bg3 = bg1.abs(mc1);

    System.out.println(bg2);/*from w  ww  .  ja v a2 s  .  c o  m*/
    System.out.println(bg3);
}

From source file:Main.java

public static void main(String[] args) {
    MathContext mc = new MathContext(3);
    BigDecimal bg1 = new BigDecimal(new BigInteger("40"), 2, mc);

}

From source file:Main.java

public static void main(String[] args) {
    MathContext mc = new MathContext(3);
    BigDecimal bg1 = new BigDecimal(new BigInteger("40"), mc);

}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("-333.3454");

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

    // perform plus on bg1 using mc
    BigDecimal bg2 = bg1.plus(mc);

    System.out.println(bg2);/*ww  w  .  j a  v  a2  s  .  com*/
}

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 w  w  w. j  a  v  a 2 s  .co m
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("99.8");
    BigDecimal bg2 = new BigDecimal("3");

    MathContext mc = new MathContext(2);

    BigDecimal bg3 = bg1.divideToIntegralValue(bg2, mc);

    System.out.println(bg3);//from   w  ww. ja v a 2s  .  c o  m
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123456");
    BigDecimal bg2 = new BigDecimal("3");

    MathContext mc = new MathContext(2);

    BigDecimal bg3 = bg1.divide(bg2, mc);

    System.out.println(bg3);/*from  w  ww. j  a v  a2 s .  co  m*/
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("143.145");
    BigDecimal bg2 = new BigDecimal("10.01");

    MathContext mc = new MathContext(2);

    BigDecimal bg[] = bg1.divideAndRemainder(bg2, mc);

    System.out.println("Quotient is " + bg[0]);
    System.out.println("Remainder is " + bg[1]);
}