Source of DollarsFirstTry.java


  1: 
  2: public class DollarsFirstTry
  3: {
  4:     /**
  5:      Outputs amount in dollars and cents notation. 
  6:      Rounds after two decimal points. 
  7:      Does not advance to the next line after output.
  8:     */
  9:     public static void write(double amount)
 10:     {
 11:         int allCents = (int)(Math.round(amount*100)); 
 12:         int dollars = allCents/100;
 13:         int cents = allCents%100;
 14:         System.out.print('$');
 15:         System.out.print(dollars);
 16:         System.out.print('.');
 17:         if (cents < 10)
 18:         {
 19:             System.out.print('0');
 20:             System.out.print(cents);
 21:         }
 22:         else
 23:             System.out.print(cents);
 24:     }
 25:     /**
 26:      Outputs amount in dollars and cents notation. 
 27:      Rounds after two decimal points. 
 28:      Advances to the next line after output.
 29:     */
 30:     public static void writeln(double amount)
 31:     {
 32:         write(amount);
 33:         System.out.println( );
 34:     }
 35: }