Java Reference
In-Depth Information
BigDecimal discountRate = new BigDecimal (".85321");
BigDecimal discountedTransaction =
transactionAmount.multiply (discountRate);
System.out.println ("discountedTransaction = " +
discountedTransaction);
// Subtract the discounted transaction evenly from accounts 2
// and 3.
BigDecimal discountDistribution =
discountedTransaction.divide (BigDecimal.valueOf(2),
// Make sure the result is the same scale as accountBalance.
accountBalance1.scale(), BigDecimal.ROUND_HALF_EVEN);
System.out.println ("discountDistribution = " +
discountDistribution);
// No need to worry about scale or lost precision here;
// the scale of the result will be the greater of the two
// BigDecimal
// operands.
accountBalance2 =
accountBalance2.subtract (discountDistribution);
accountBalance3 =
accountBalance3.subtract (discountDistribution);
System.out.println ("accountBalance2 = " +
accountBalance2);
System.out.println ("accountBalance3 = " +
accountBalance3);
// Now you have to set the number's scale so that you can fit it
// into a
// print column. You want two decimal positions to be printed.
BigDecimal printAccountBalance =
accountBalance1.setScale (2,
BigDecimal.ROUND_HALF_UP);
System.out.println ("printAccountBalance = " +
printAccountBalance);
13. Compile and run this class. Your output window should look like this:
...
These are words in a sentence
accountBalance1 = 12445.678
discountedTransaction = 85.3210000
discountDistribution = 42.660
accountBalance2 = 833.88321
accountBalance3 = -41.660
printAccountBalance = 12445.68
Search WWH ::




Custom Search