【发布时间】:2016-08-21 04:07:21
【问题描述】:
这是程序。它旨在根据工作时间、小时费率、预扣税款等计算员工的净工资。它计算正确,但双点精度格式会四舍五入最后一位小数,失去计算的准确性。
示例输入:
姓名:史密斯
本周工作时间:10
每小时费率:6.75
联邦预扣税:20%
州税预扣:9%
输出:
总结
员工:史密斯
总工资:67.50
联邦预扣税:13.50
州预扣税:6.08
总扣除额:19.58
净工资:47.93
import java.util.*;
public class Payroll
{
static Scanner key = new Scanner(System.in);
public Payroll()
{
System.out.print("Name: ");
String name = key.next();
System.out.print("Hours worked this week: ");
int hoursWorked = key.nextInt();
System.out.print("Hourly rate: ");
double payRate = key.nextDouble();
double payPreTax = hoursWorked * payRate;
System.out.print("Federal tax withhold: ");
String fedTaxStr = key.next().replace("%", "");
double fedTax = ((Double.parseDouble(fedTaxStr)) / 100) * payPreTax;
System.out.print("State tax withold: ");
String stateTaxStr = key.next().replace("%", "");
double stateTax = ((Double.parseDouble(stateTaxStr)) / 100) * payPreTax;
double amountWithheld = fedTax + stateTax;
double payPostTax = payPreTax - amountWithheld;
System.out.printf("\nSummary\n\nEmployee: " + name + "\nGross Pay: %.2f\nFederal Withholding: %.2f\nState Withholding: %.2f\nTotal Deduction: %.2f\nNet Pay: %.2f", payPreTax, fedTax, stateTax, amountWithheld, payPostTax);
}
public static void main(String[] args)
{
new Payroll();
}
}
请见谅;我是一年级。
【问题讨论】:
-
您的预期输出是什么?
-
@Shahid 摘要员工:Smith Gross 工资:67.50 联邦预扣税:13.50 州预扣税:6.07 总扣除额:19.57 净工资:47.93
-
您应该不在货币方面使用浮点运算。使用
BigDecimal。
标签: java