Java string.format 加倍问题

java string.format doubles issue

提问人:metamorphisis 提问时间:7/4/2020 最后编辑:metamorphisis 更新时间:11/16/2023 访问量:84

问:

只需要帮助格式化字符串,如下所示:

Here is your tax breakdown:
Income            $25,000.00
Dependants                 1
----------------------------
Federal Tax        $4,250.00
Provincial Tax     $1,317.75
============================
Total Tax          $5,567.75"

这是我的代码,请帮我用空格和小数格式化

import java.util.Scanner;

public class HelloWorld {
    public static void main(String args[]) {
        double income, fedTax, provTax;
        int dependents;

        Scanner input = new Scanner(System.in);
        System.out.print("Please enter your taxable income: ");
        income = input.nextInt();

        System.out.println();
        System.out.print("Please enter your number of dependents: ");

        dependents = input.nextInt();
        System.out.println();

        if (income <= 29590) {
            fedTax = 0.34 * income;
        }

        else if (income <= 59179.99) {
            fedTax = 0.34 * 29590 + 0.26 * (income - 29590);
        }

        else {
            fedTax = 0.34 * 29590 + 0.26 * 29590 + (0.29 * (income - 59180));
        }

        double base = 42.5 * fedTax;
        double deductions = 160.50 + 328 * dependents;
        provTax = base - deductions;
        if (base < deductions) {
            provTax = 0;
        }
        else {
            provTax = base - deductions;
        }

        double totalTax = fedTax + provTax;

        System.out.println("Here is your tax breakdown: ");
        System.out.println(String.format(" Income%,10.d", income));
        System.out.println(String.format(" Dependants%10.d", dependents));
        System.out.println("-------------------------");
        System.out.println(String.format(" Federal Tax%,10.2f", fedTax));
        System.out.println(String.format("Provincial tax%,10.2f", provTax));
        System.out.println("=========================");
        System.out.println(String.format(" Total Tax%,10.2f", totalTax));




    }
}

发生的主要问题之一是因为我试图使用 system.out 和 printstream 在我的测试中插入输入,以对这个程序的测试输入进行排序。使用此功能,当函数调用的顺序覆盖程序的缓冲区时,允许在一行中输入多个输入要容易得多。

public class StringInputProvider implements InputProvider {
    private StringBuilder inputBuilder = new StringBuilder();
    private int currentIndex = 0;

    public StringInputProvider(String... inputs) {
        for(String input : inputs){
            this.inputBuilder.append(input).append(System.lineSeparator());
        }
    }


    public void addInput(String input) {
        inputBuilder.append(input).append(System.lineSeparator());
    }

    public String nextLine() {
        int nextLineStart = currentIndex;
        while (currentIndex < inputBuilder.length() && inputBuilder.charAt(currentIndex) != '\n') {
            currentIndex++;
        }
        String line = inputBuilder.substring(nextLineStart, currentIndex).trim();
        currentIndex++; // Move past the newline character
        return line;
    }
}

编辑:为新版本添加了新值

爪哇岛

评论


答:

1赞 DevilsHnd - 退した 7/4/2020 #1

如果收入变量被声明为双精度数据类型,那么你就不能期望使用 Scanner#nextInt() 方法填充该变量,该方法期望用户输入一个 Integer 值,而你想要输入一个双精度(浮点)值。至少你要使用 Scanner#nextDouble() 方法。

我不认为你对省税的计算是正确的。如果是这样,那么我肯定不想处理那个账单。我不会试图弄清楚这一点,但这一开始就很可疑:

double base = 42.5 * fedTax;
double deductions = 160.50 + 328 * dependents;
provTax = base - deductions;       // deductions removed from base here...
if (base < deductions) {
    provTax = 0;
}
else {
    provTax = base - deductions;  // and yet again here....hmmmm
}

为了获得你想要的格式,你可能希望同时使用 String#format() 方法和 DecimalFormat#format() 方法,并可能使用它的几个其他方法阅读尾注),例如:

// Set the numerical format you want.
java.text.DecimalFormat decimalFormat = new  java.text.DecimalFormat("0.00");
decimalFormat.setGroupingUsed(true); // You want comma grouping
decimalFormat.setGroupingSize(3);    // Comma every 3 digits
    
System.out.println("Here is your tax breakdown: ");
System.out.println(String.format("Income %20s", "$" + decimalFormat.format(income)));
System.out.println(String.format("Dependants %16d", dependents));
System.out.println("---------------------------");
System.out.println(String.format("Federal Tax %15s", "$" + decimalFormat.format(fedTax)));
System.out.println(String.format("Provincial Tax %12s", "$" + decimalFormat.format(provTax)));
System.out.println("===========================");
System.out.println(String.format("Total Tax %17s", "$" + decimalFormat.format(totalTax)));

经过上述修改和对收入提示的修复(使用 Scanner#nextDouble() 方法)的整个代码将如下所示:

double income, fedTax, provTax;
int dependents;

Scanner input = new Scanner(System.in);
System.out.print("Please enter your taxable income: ");
income = input.nextDouble();

System.out.println();
System.out.print("Please enter your number of dependents: ");

dependents = input.nextInt();
System.out.println();

if (income <= 29590) {
    fedTax = 0.17 * income;
}
else if (income <= 59179.99) {
    fedTax = 0.17 * 29590 + 0.26 * (income - 29590);
}
else {
    fedTax = 0.17 * 29590 + 0.26 * 29590 + (0.29 * (income - 59180));
}

double base = 42.5 * fedTax;
double deductions = 160.50 + 328 * dependents;
provTax = base - deductions;
if (base < deductions) {
    provTax = 0;
}
else {
    provTax = base - deductions;
}

double totalTax = fedTax + provTax;

// Set the numerical format you want.
java.text.DecimalFormat decimalFormat = new java.text.DecimalFormat("0.00");
decimalFormat.setGroupingUsed(true); // You want comma grouping
decimalFormat.setGroupingSize(3);    // Comma every 3 digits
  
System.out.println("Here is your tax breakdown: ");
System.out.println(String.format("Income %20s", "$" + decimalFormat.format(income)));
    System.out.println(String.format("Dependents %16d", dependents));
    System.out.println("---------------------------");
    System.out.println(String.format("Federal Tax %15s", "$" + decimalFormat.format(fedTax)));
    System.out.println(String.format("Provincial Tax %12s", "$" + decimalFormat.format(provTax)));
    System.out.println("===========================");
    System.out.println(String.format("Total Tax %17s", "$" + decimalFormat.format(totalTax)));

如果现在要运行此代码,则应在控制台窗口中看到以下内容(如果提供相同的值):

Please enter your taxable income: 25500.54

Please enter your number of dependents: 1

Here is your tax breakdown: 
Income           $25,500.54
Dependents                1
---------------------------
Federal Tax       $4,335.09
Provincial Tax  $183,752.90
===========================
Total Tax       $188,087.99

25.5 grand的税单。:/

注意:如果使用以下命令初始化 decimalformat 变量,则可以避免使用 DecimalFormat#setGroupingUsed() 和 DecimalFormat#setGroupingSize() 方法:

DecimalFormat decimalFormat = new DecimalFormat("#,###,##0.00");

评论

0赞 metamorphisis 7/4/2020
哈哈哈,错了,你是对的,我的好先生,我不小心留下了一个计算 x 45 而不是它的 45%。谢谢你的解决方案