应该对latter2格式化一下。代码如下:
import java.util.*;
public class text3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double latter;
System.out.println("收入为:");
double money;
do {
money = s.nextDouble();
if (money < 0)
System.out.println("错误,请重新输入");
} while (money < 0);
double former1 = money - 1500;
double former2 = money - 4500;
double former3 = money - 9000;
double former4 = money - 35000;
double former5 = money - 55000;
double former6 = money - 80000;
if ((0 <= money) & (money <= 1500))
latter = money * 0.03;
else if ((1500 < money) & (money <= 4500))
latter = 1500 * 0.03 + former1 * 0.1;
else if ((4500 < money) & (money <= 9000))
latter = 1500 * 0.03 + 3000 * 0.1 + former2 * 0.2;
else if ((9000 < money) & (money <= 35000))
latter = 1500 * 0.03 + 3000 * 0.1 + 4500 * 0.2 + former3 * 0.25;
else if ((35000 < money) & (money <= 55000))
latter = 1500 * 0.03 + 3000 * 0.1 + 4500 * 0.2 + 26000 * 0.25
+ former4 * 0.3;
else if ((55000 < money) & (money <= 80000))
latter = 1500 * 0.03 + 3000 * 0.1 + 4500 * 0.2 + 26000 * 0.25
+ 20000 * 0.3 + former5 * 0.35;
else
latter = 1500 * 0.03 + 3000 * 0.1 + 4500 * 0.2 + 26000 * 0.25
+ 20000 * 0.3 + 25000 * 0.35 + former6 * 0.45;
double latter2 = ((int) ((latter + 0.005) * 100)) / 100;
System.out.println("需要缴纳的个人所得税为:" + new java.text.DecimalFormat("#.00").format(latter2));
}
}
用DecimalFormat
直接printf
System.out.printf("%.2f", 3.1415926);
输出 3.14