Advertisement
Guest User

NumberFormat

a guest
Feb 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.text.NumberFormat;
  3. public class Purchase
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         final double tax_rate = 0.06;
  8.         int quantity;
  9.         double subtotal, tax, totalcost, unitPrice;
  10.         Scanner scan = new Scanner(System.in);
  11.        
  12.         NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
  13.         NumberFormat fmt2 = NumberFormat.getPercentInstance();
  14.        
  15.         System.out.println("Enter the quantity: ");
  16.         quantity = scan.nextInt();
  17.        
  18.         System.out.println("Enter the unit price: ");
  19.         unitPrice = scan.nextDouble();
  20.        
  21.         subtotal = quantity * unitPrice;
  22.         tax = subtotal * tax_rate;
  23.         totalcost = subtotal * tax;
  24.        
  25.         System.out.println("Subtotal "+ fmt1.format(subtotal));
  26.         System.out.println("Tax: "+ fmt1.format(tax)+ " at"+ fmt2.format(tax_rate));
  27.         System.out.println("Total: "+ fmt1.format(totalcost));
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement