import java.util.Scanner;
public class Taxes {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.printf("Enter the employees first name: ");
Scanner input = new Scanner(System.in);
String fName = input.nextLine();
System.out.printf("Enter the employees last name: ");
String lName = input.nextLine();
System.out.printf("Enter the hours worked for the week: ");
double hours = input.nextDouble();
System.out.printf("Enter the hourly pay rate: ");
double pay = input.nextDouble();
double gross = hours * pay;
System.out.printf("Enter the federal tax withholding: ");
double fed = input.nextDouble();
double fTax = gross * fed;
System.out.printf("Enter the state tax withholding: ");
double state = input.nextDouble();
double sTax = gross * state;
double Ttax = sTax + fTax;
double net = gross - Ttax;
System.out.printf(
"Employee Name:%s %s\n\nHours Worked:%s hours\n\nPay Rate:$%.2f\n\nGross pay:$%.2f\n\nDeductions: \n\n\tFederal Withholding:(%.2f%%)$%.2f \n\n"
+ "\tState Withholding:(%.2f%%)$%.2f\n\n\tTotal Witholding:$%.2f\n\nNet Pay:$%.2f",
fName, lName, hours, pay, gross, fed, fTax, state, sTax, Ttax, net);
input.close();
}
}
我需要声明另外两个变量,以使联邦和州的预扣税显示为百分比。
示例它们显示为(00.20%)我需要它们返回整体百分比,例如(20.00%)
我尝试在底部声明新变量,例如:
statewit = sTax * 100;
fedwit = fTax * 100;
以获得我想要的回报百分比,但是它倾向于将最后的总和添加到网络中。
任何帮助将不胜感激,谢谢!
最佳答案
尝试这个。
double percent=12.34;
System.out.printf("%.2f%%", percent);
// or in different convention "percent as number *100"
System.out.printf("%.2f%%", percent*100.0);
编辑:您的问题可以分为两部分:
使用数字的惯例(正常或百分位数* 100)
实际格式化为字符串
顺便说一句,您的代码很长,几乎没有格式。
Java没有用于百分比值的特殊类型。类型:double,BigDecimal可以与他的行为一起使用,或者如果程序员保留整数约定,也可以使用整数类型
编辑:谢谢Costis Aivalis,逗号已纠正:)