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("10");
    BigDecimal bg2 = new BigDecimal("20");

    int res = bg1.compareTo(bg2); // compare bg1 with bg2

    String str1 = "Both values are equal ";
    String str2 = "First Value is greater ";
    String str3 = "Second value is greater";

    if (res == 0)
        System.out.println(str1);
    else if (res == 1)
        System.out.println(str2);
    else if (res == -1)
        System.out.println(str3);
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123");
    BigDecimal bg2 = new BigDecimal("0");
    BigDecimal bg3 = new BigDecimal("-12");

    int i1 = bg1.signum();
    int i2 = bg2.signum();
    int i3 = bg3.signum();

    String str1 = "The Result of Signum function on " + bg1 + " is " + i1;
    String str2 = "The Result of Signum function on " + bg2 + " is " + i2;
    String str3 = "The Result of Signum function on " + bg3 + " is " + i3;

    // print i1,i2,i3 values
    System.out.println(str1);/*w  w w .  j  a  va 2 s  .c  o m*/
    System.out.println(str2);
    System.out.println(str3);
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal(new BigInteger("40"));

}

From source file:Main.java

public static void main(String[] args) {
    showPrice(new BigDecimal("123.456"));
    showPrice(new BigDecimal("11.12"));
    showPrice(new BigDecimal("10.5"));
    showPrice(new BigDecimal("1.5"));
    showPrice(new BigDecimal("0.5"));
}

From source file:Main.java

public static void main(String[] args) {
    BigDecimal bg1 = new BigDecimal("2426");
    // assign the BigIntegerExact value of bg1 to i1
    BigInteger i1 = bg1.toBigIntegerExact();

    System.out.println("BigInteger value of " + bg1 + " is " + i1);
}

From source file:Main.java

public static void main(String[] args) {
    BigDecimal decimalA = new BigDecimal("123456789012345");
    BigDecimal decimalB = new BigDecimal("10");

    decimalA = decimalA.add(decimalB);/*from www.  j  a  va2s.c om*/
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.multiply(decimalB);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.subtract(decimalB);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.divide(decimalB);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.pow(2);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.negate();
    System.out.println("decimalA = " + decimalA);
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("235.738");

    BigInteger i1 = bg1.toBigInteger();

    System.out.println("BigInteger value of " + bg1 + " is " + i1);
}

From source file:Main.java

public static void main(String[] args) {

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

    // divide bg1 with bg2 with 3 scale
    BigDecimal bg3 = bg1.divide(bg2, 3, RoundingMode.CEILING);

    System.out.println(bg3);/*from  www .  j a va 2  s . c  o  m*/
}

From source file:Main.java

public static void main(String[] args) {

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

    // divide bg1 with bg2 rounding up
    BigDecimal bg3 = bg1.divide(bg2, RoundingMode.UP);

    System.out.println(bg3);/*  w w w  .j a v a2s .  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);/* w ww. jav  a  2 s  .c  o m*/
}