Project 13 - BigDecimal

due 11:59pm Fri Feb 26th, 2016
Project id  bigdecimal

  1. Objects are non-primitive values with the structure given by some class.
  2. Objects are created by "new".
  3. Constructors are special methods to create objects; they have the same name as the class.
  4. Constructors can have arguments and can be overloaded.
  5. Methods associated with an object can be invoked using the dot syntax.
  6. There are many predefined classes in the Java API which can be used to create useful objects.

BigDecimal Class

To be able to write Java programs that deal with finances, you need to know about the BigDecimal class and numeric formatting. BigDecimal will deal with double numbers as an integer with fix position of the decimal dot. There are 16 constructors for the class.

What you should do is create the BigDecimal directly with the string ".35" as shown here:

  BigDecimal d = new BigDecimal(".35");

  System.out.println(".35 = " + d);

resulting in the following output:

  .35 = 0.35

Since you can't necessarily store the value of a BigDecimal in a primitive object like a double, it is best to create your BigDecimal objects from a String. To demonstrate this error, here's a simple example:

  double dd = .35;

  BigDecimal d = new BigDecimal(dd);

  System.out.println(".35 = " + d);

The output is not what you might have expected:

  .35 = 0.34999999999999997779553950749686919152736663818359375

In the first case, the String argument to the BigDecimal constructor determines the value of the object created. To display the amount, you can use the BigDecimal class's toString() method: 


If you call println with an object argument directly, it calls object's toString() method first and then show the result of this method.

BigDecimal operations

BigDecimal methods for adding and subtracting numbers are add() and subtract(), respectively. For example, to add 1,115.37 and 115.37, we could do the following:

BigDecimal balance = new BigDecimal("1115.37"); 
BigDecimal transaction = new BigDecimal("115.37"); 
BigDecimal newBalance = balance.add(transaction);

The BigDecimal's newBalance object now holds the value of 1,230.74. Similarly, we can subtract , multiplying and dividing with BigDecimal's subtract(), multiply(), and divide() methods.

    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");
    // Create via a long
    BigDecimal bd2 = new BigDecimal(123L);

    BigDecimal bd3;
    bd1 = bd1.add(bd2);
    bd1 = bd1.multiply(bd2);
    bd3 = bd1.subtract(bd2);
    bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
    bd1 = bd1.negate();

    bd1 = bd1.pow(n);

Precision and Scale

in a BigDecimal number, Scale is the number of digits after decimal point, you can think of a scale as a divisor to the unscaled number. Internally, BigDecimal will keep an integer (a long long integer) with a divisor or scale and in all operations it will use (unscaledValue × 10-scale). Precision is the number of digits in the unscaled number. You cannot set precision, it will be computed automatically, but you can change the scale using setScale method or get it using scale method. To set the number of decimal places to two and specify that fractions half and greater will be rounded up, we can write:

d2 = d2.setScale(2, BigDecimal.ROUND_HALF_UP);

For more on BigDecimal  Class: ( )


1-  Write a java program that takes two command line arguments (L-loan amount, I-interest rate per period) and reads bunch of payments from the standard input steam and shows the result in standard output.  Each payment represents the passage of period of the loan.  For each payment the program computes the interest on the current balance, adds it to the balance, and then subtracts the payment from the balance. The interest is the rate times the balance rounded to the nearest penny.  Each time the string “Balance” appears in the input the program prints the balance. For positive balance print the word 'left' after the number, for negative balance print 'over', and for zero print 0.00 and nothing afterewards.

Note: set the scale to always 2 digits after decimal point regardless of the input strings.

Sample Command Line:
java Mortgage 100.00 0.01

Sample Input


Sample Output
Balance: 100.00 left
Balance: 100.00 left
Balance: 71.91 left
Balance: 27.37 over