简体   繁体   English

java string.format 双打问题

[英]java string.format doubles issue

just need help with formatting a string to look like this:只需要帮助将字符串格式化为如下所示:

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"

heres my code, please help me format as above with spaces and decimals这是我的代码,请帮我格式化上面的空格和小数

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.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;

        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));




    }
}

please ignore down here this is just to let me post because there isnt enough words, even though my question does not to be detailed:(请忽略这里,这只是为了让我发帖,因为没有足够的文字,即使我的问题不详细:(

If the income variable is declared a double data type then you can't expect to fill that variable using the Scanner#nextInt() method which is expecting the User to enter an Integer value whereas you want to enter a double (floating point) value.如果收入变量被声明为双精度数据类型,那么您不能期望使用Scanner#nextInt()方法填充该变量,该方法期望用户输入 Integer 值,而您想输入双精度(浮点)值. At the very least you want to use the Scanner#nextDouble() method.至少您想使用Scanner#nextDouble()方法。

I don't think your calculations for Provincial Tax is correct.我认为您对省税的计算不正确。 If it is then I sure wouldn't want to deal with that bill.如果是,那么我肯定不想处理该法案。 I'm not going to try and figure that one out but this is rather suspect to start with:我不打算尝试解决这个问题,但这是相当可疑的开始:

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
}

To get the formatting you want you would want to possibly utilize both the String#format() method along with the DecimalFormat#format() method and possibly a couple of its' other methods (read end note), for example:要获得您想要的格式,您可能需要同时使用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)));

Your entire code with the above modification and the fix for the income prompt (using the Scanner#nextDouble() method) would look something like this:您的整个代码经过上述修改和收入提示的修复(使用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)));

If you were to run this code now, you should see in the Console Window the following (if you supply the same values):如果您现在运行此代码,您应该在控制台 Window 中看到以下内容(如果您提供相同的值):

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

What a tax bill for 25.5 grand. 25.5盛大的税单。 :/ :/

Note: You can eliminate the use of the DecimalFormat#setGroupingUsed() and the DecimalFormat#setGroupingSize() methods if you initialize your decimalformat variable with:注意:如果您使用以下方法初始化您的 decimalformat 变量,则可以消除使用DecimalFormat#setGroupingUsed()DecimalFormat#setGroupingSize()方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM