Source of DollarFormatFirstTry.java


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