Operating with Big Decimal Values - Java Language Basics

Java examples for Language Basics:BigDecimal

Description

Operating with Big Decimal Values

Demo Code

import java.math.BigDecimal;

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

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

    bd1 = bd1.add(bd2);/*from w  ww.  j  ava2  s . c o  m*/
    System.out.println(bd1);
    bd1 = bd1.multiply(bd2);
    System.out.println(bd1);
    bd1 = bd1.subtract(bd2);
    System.out.println(bd1);
    bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
    System.out.println(bd1);
    bd1 = bd1.negate();
    System.out.println(bd1);
  }
}

Result


Related Tutorials