This question already has answers here:
How to round a number to n decimal places in Java
(31个答案)
5年前关闭。
我需要将输出浮到小数点后两位,因为输出就是金钱。例如,如果我运行:
如果您单身,请输入1。如果您已结婚,请输入2
1个
输入您的应税收入
27060.34
联邦所得税:$ 4060.3435
我需要联邦所得税来输出$ 4060.34而不是$ 4060.3435
我的代码:
(31个答案)
5年前关闭。
我需要将输出浮到小数点后两位,因为输出就是金钱。例如,如果我运行:
如果您单身,请输入1。如果您已结婚,请输入2
1个
输入您的应税收入
27060.34
联邦所得税:$ 4060.3435
我需要联邦所得税来输出$ 4060.34而不是$ 4060.3435
我的代码:
import static org.junit.Assert.*;
import java.util.Scanner;
import org.junit.Test;
public class IRS {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter 1 if you are single. Enter 2 if you are married");
int martialstatus = scan.nextInt();
if (martialstatus == 1) {
Scanner scan2 = new Scanner(System.in);
System.out.println("Enter your taxable income");
double income = scan2.nextDouble();
if ((income > 0) && (income <= 27050.00)) {
System.out.println("Federal Income Tax:$" + (income * .15));
}
if ((income > 27050.00) && (income <= 65550.00)) {
System.out.println("Federal Income Tax:$" + (4057.50 + (.275 * (income - 27050))));
}
if ((income > 65550.00) && (income <= 136750.00)) {
System.out.println("Federal Income Tax:$" + (14645.00 + (.305 * (income - 65550.00))));
}
if ((income > 136750.00) && (income <= 297350.00)) {
System.out.println("Federal Income Tax:$" + (36361.00 + (.355 * (income - 136750.00))));
}
if (income > 297350.00) {
System.out.println("Federal Income Tax:$" + (93374.00 + (.391 * (income - 297350.00))));
}
} else if (martialstatus == 2) {
Scanner scan3 = new Scanner(System.in);
System.out.println("Enter your taxable income");
double income2 = scan3.nextDouble();
if (income2 <= 45200.00) {
System.out.println("Federal Income Tax:$" + (.15 * income2));
}
if ((income2 > 45200.00) && (income2 <= 109250.00)) {
System.out.println("Federal Income Tax:$" + (6780.00 + (.275 * (income2 - 45200))));
}
if ((income2 > 109250.00) && (income2 <= 166500.00)) {
System.out.println("Federal Income Tax:$" + (24393.75 + (.305 * (income2 - 109250.00))));
}
if ((income2 > 166500.00) && (income2 <= 297350.00)) {
System.out.println("Federal Income Tax:$" + (41855.00 + (.355 * (income2 - 166500.00))));
}
if (income2 > 297350.00) {
System.out.println("Federal Income Tax:$" + (88306.00 + (.391 * (income2 - 297350.00))));
}
} else {
System.out.println("Error. Try again");
}
}
}
最佳答案
如果您对C样式的打印感到满意(这是我在这里推荐的建议),则可以使用System.out.printf(),它与C的printf()非常相似。
System.out.printf ("Federal Income Tax: $%.2f", federalTax);
10-06 07:09