Example usage for java.math BigDecimal BigDecimal

List of usage examples for java.math BigDecimal BigDecimal

Introduction

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

Prototype

public BigDecimal(long val) 

Source Link

Document

Translates a long into a BigDecimal .

Usage

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);/*from   w  w  w .  j a v  a  2s .  com*/
}

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);/* w ww . j  a  v a  2  s  . co  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Stream.of(new BigDecimal("1.2"), new BigDecimal("3.7")).mapToDouble(BigDecimal::doubleValue).average()
            .ifPresent(System.out::println);
}

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]);
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.12345678");

    // set scale of bg1 to 2 in bg2
    // 0 specifies ROUND_UP
    BigDecimal bg2 = bg1.setScale(2, RoundingMode.UP);

    String str = bg1 + " after changing the scale to 2 and rounding is " + bg2;

    System.out.println(str);/*from www .ja va2s  . c  o  m*/
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.12678");

    // set scale of bg1 to 2 in bg2 using floor as rounding mode
    BigDecimal bg2 = bg1.setScale(2, RoundingMode.FLOOR);

    String str = bg1 + " after changing the scale to 2 and rounding is " + bg2;

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

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1, bg2, bg3, bg4;

    bg1 = new BigDecimal("123");
    bg2 = new BigDecimal("1.23");

    // assign ulp value of bg1, bg2 to bg3, bg4
    bg3 = bg1.ulp();/* w  w  w  . j a v  a  2s.c o  m*/
    bg4 = bg2.ulp();

    System.out.println("ULP value of " + bg1 + " is " + bg3);
    System.out.println("ULP value of " + bg2 + " is " + bg4);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int decimalPlaces = 2;
    BigDecimal bd = new BigDecimal("123456789.0123456890");

    bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_DOWN);
    String string = bd.toString();
}

From source file:Main.java

public static void main(String[] args) {

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

    // value before applying abs
    System.out.println("Value is " + bg1);

    // assign absolute value of bg1 to bg2
    BigDecimal bg2 = bg1.abs();//from w  w w.  ja va2s .co  m

    // print bg2 value
    System.out.println("Absolute value is " + bg2);
}

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);/* ww  w  .jav  a2 s. c  om*/
}