// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 5 - Computation // Program to demonstrate methods in the Math class // MathClassDemo.java 1 JAN 05 import java.text.NumberFormat; public class MathClassDemo { public static void main(String args[]) { // declare variables float balance = 1000F; float apr = .08F; int months = 6; double payment; payment = (balance * (apr / 12))/ (1 - 1 / Math.pow ((1 + apr/12), months)); float roundedPayment = roundOff(payment); // round to 2 decimals System.out.println ("Payment = " + payment); System.out.println ("Rounded Payment = " + roundedPayment); } // end main // roundOff method static public float roundOff (double value) { value = value * 100; // move decimal 2 places to right // Math.round returns long - recast to float float roundedValue = (float) Math.round(value); roundedValue = roundedValue / 100; // move decimal back to left return roundedValue; } // end roundOff } // end MathClassDemo