-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberFormat.java
More file actions
48 lines (28 loc) · 1.22 KB
/
NumberFormat.java
File metadata and controls
48 lines (28 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// This program shows how to use NumberFormat and DecimalFormat
package com.company;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
final double TAX_RATE = 0.06;
int quantity;
double subtotal, tax, totalCost, unitPrice;
Scanner scan = new Scanner(System.in);
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
NumberFormat fmt2 = NumberFormat.getPercentInstance();
NumberFormat fmt3 = NumberFormat.getNumberInstance();
System.out.println("Enter the quantity: ");
quantity = scan.nextInt();
System.out.println("Enter the unit price: ");
unitPrice = scan.nextDouble();
subtotal = quantity * unitPrice;
tax = subtotal * TAX_RATE;
totalCost = subtotal + tax;
System.out.println("Subtotal: " + fmt1.format(subtotal));
System.out.println("Tax: " + fmt1.format(tax) + " at " + fmt2.format(TAX_RATE) + " tax rate.");
System.out.println("Total: " + fmt3.format(totalCost));
DecimalFormat dfrmt = new DecimalFormat("0.00");
System.out.println(dfrmt.format(Math.PI));
}
}