【发布时间】:2014-10-29 02:00:04
【问题描述】:
课堂作业基本上是有一个对话框,询问用户员工姓名、小时工资和一周的小时数。它将其输出到文本文件。我完成了那部分,老师说他发现输出有 4 位小数,例如。 34.5500
我在这里寻求帮助,我如何只能显示两个小数点,所以它是 34.55。这是我编译的代码。
package out.put.of.a.file;
import javax.swing.JOptionPane;
public class OutPutOfAFile
{
public static void main(String[] args)
{
OutputFile payFile;
payFile = new OutputFile("payroll.txt");
String name;
Double pay, hours;
String answer;
Keyboard k;
k = new Keyboard();
do
{
name = JOptionPane.showInputDialog("Enter the employee's name:");
pay = Double.parseDouble(JOptionPane.showInputDialog("Enter the employee's hourly wage:"));
hours = Double.parseDouble(JOptionPane.showInputDialog("Enter the employee's total hours:"));
payFile.writeString(name);
payFile.writeDouble(pay);
payFile.writeDouble(hours);
payFile.writeEOL();
answer = JOptionPane.showInputDialog("Do you have another employee to enter? (yes/no)");
} while (answer.equalsIgnoreCase("yes"));
payFile.close();
}
}
【问题讨论】:
-
你可以在
NumberFormat做一些研究...
标签: java file-io keyboard decimal rounding