【发布时间】:2015-01-22 04:47:04
【问题描述】:
我想弄清楚如何将输出格式化为货币。由于我是 Java 的初学者,尽管我愿意接受所有建议,但我想要最简单的解决方案。
public static void main(String[] args)
{
int numUnits;
double price;
String item;
Scanner scan = new Scanner(System.in);
// get the data
System.out.println("How many units?");
numUnits = scan.nextInt();
System.out.println("What is the price?");
price = scan.nextDouble();
scan.nextLine();
System.out.println("What is the name of the item?");
item = scan.nextLine();
//calculations
double totalCost = numUnits * price;
System.out.println(numUnits + " units of "+ item + " were purchased for $"+
price + " each for a total cost of $" + totalCost);
}
}
【问题讨论】:
标签: java format double decimal currency