Tuesday, July 26, 2016

BigDecimal sample code

The code written below is an example for BigDecimal.

-----------------------------------------------------------------------------
import java.math.BigDecimal;

public class HelloWorld
{
  public static void main(String[] args)
  {
    BigDecimal one = new BigDecimal("1.00");
    BigDecimal two = new BigDecimal("2.00");
    BigDecimal three = new BigDecimal("3.00");

    BigDecimal addition = one.add(two);
    BigDecimal subtraction = one.subtract(two);
    BigDecimal multiplication = two.multiply(three);
    BigDecimal division = one.divide(three, 2, BigDecimal.ROUND_HALF_UP);
   
    System.out.println("addition: " + addition);
    System.out.println("subtraction: " + subtraction);
    System.out.println("multiplication: " + multiplication);
    System.out.println("division: " + division);
  }
}
-----------------------------------------------------------------------------