44
NumberFormat fmt1 =
NumberFormat.getCurrencyInstance();
NumberFormat fmt2 =
NumberFormat.getPercentInstance();
System.out.print ("Enter the quantity: ");
quantity = scan.nextInt();
System.out.print ("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));
System.out.println ( "Total: " +
fmt1.format(totalCost));
}
}
Рисунок 2.12 – Пример форматированного вывода
Достарыңызбен бөлісу: