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

    // assign unscaledValue of bg1,bg2 to bi1,bi2
    BigInteger bi1 = bg1.unscaledValue();
    BigInteger bi2 = bg2.unscaledValue();

    String str1 = "The Unscaled Value of " + bg1 + " is " + bi1;
    String str2 = "The Unscaled Value of " + bg2 + " is " + bi2;

    System.out.println(str1);/*from w  w w.j a v  a 2  s . c om*/
    System.out.println(str2);
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("12.345");
    BigDecimal bg2 = new BigDecimal("23.456");

    System.out.println("Object Value is " + bg1);
    System.out.println("Augend value is " + bg2);

    // create MathContext object with 4 precision
    MathContext mc = new MathContext(4);

    // perform add operation on bg1 with augend bg2 and context mc
    BigDecimal bg3 = bg1.add(bg2, mc);

    System.out.println("Result is " + bg3);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");

    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);

    bd1 = bd1.add(bd2);/*  w w  w.  ja v  a  2  s .  co m*/

}

From source file:Main.java

public static void main(String[] args) {

    // assign values to bg1 and bg2
    BigDecimal bg1 = new BigDecimal("-128");
    BigDecimal bg2 = new BigDecimal("48");

    byte i1 = bg1.byteValueExact();
    byte i2 = bg2.byteValueExact();

    // print i1,i2 values
    System.out.println(i1);/* w w w. j a  va 2  s . co  m*/
    System.out.println(i2);
}

From source file:Main.java

public static void main(String args[]) {
    double r = 3.1234567;
    int decimalPlace = 2;
    BigDecimal bd = new BigDecimal(r);
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_UP);
    r = bd.doubleValue();/*w  w  w .  j a va  2 s  .c o  m*/
    System.out.println(r);
}

From source file:Main.java

public static void main(String[] args) {
    BigDecimal payment = new BigDecimal("12345.67");
    NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
    double doublePayment = payment.doubleValue();
    String s = n.format(doublePayment);
    System.out.println(s);//from   w  w w. j a  v a  2 s . c o  m
}

From source file:Main.java

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

    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");

    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);

    bd1 = bd1.add(bd2);/*  w  w  w  .  j ava2s .c  om*/
    bd1 = bd1.multiply(bd2);
    bd1 = bd1.subtract(bd2);
    bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
    bd1 = bd1.negate();
}

From source file:Main.java

public static void main(String[] args) {
    double operation = 890.0 / 1440.0;
    BigDecimal big = new BigDecimal(operation);
    big = big.setScale(4, RoundingMode.HALF_UP);
    double d2 = big.doubleValue();
    System.out.println(String.format("operation : %s", operation));
    System.out.println(String.format("scaled : %s", d2));
}

From source file:Main.java

public static void main(String[] args) {

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

    BigDecimal bg1 = new BigDecimal("2.17");

    BigDecimal bg2 = bg1.pow(3, mc);

    System.out.println(bg2);//from   w  w  w.  ja  va  2  s .  c o  m
}

From source file:Main.java

public static void main(String[] args) {

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

    BigDecimal bg1 = new BigDecimal("1.230");
    BigDecimal bg2 = new BigDecimal("4.560");

    // multiply bg1 with bg2 using mc
    BigDecimal bg3 = bg1.multiply(bg2, mc);

    System.out.println(bg3);//from  w  ww  .  j a  va2s  .c om
}