Question

The program I am trying to make requires double "income" to be set to 2 decimal places after the program has been run.
Below shows the way I have set up, but after I run the program, it still either drops the zero or goes over. What could be the problem?

    println("Please enter your filing status");
    println("Enter 0 for single filers,");
    println("      1 for married filing jointly,");
    println("      2 for married filing seperately,");
    println("      3 for head of household");
    int status = readInt("Status: ");
    double income = readDouble("Please enter your taxable income: ");
    DecimalFormat df = new DecimalFormat("#.00");
    System.out.print(df.format(income));

if ((status == 0) && (income <= SINGLE_BRACKET1))
    {
        println("You owe: $" + (income * .1));
    }
else if  ((status == 0) && (income <= SINGLE_BRACKET2))
    {
        println("You owe: $" + ((SINGLE_BRACKET1 * .1) + 
                (income - SINGLE_BRACKET1) * .15));

    }

No correct solution

OTHER TIPS

Not sure if I understood your question, but you have to format every output:

        println("You owe: $" + df.format(income * .1));

or

        println(String.format("You owe: $%.2f", income * .1));

Use a pattern that forces the format to two decimal places:

DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
df.setMaximumFractionalPlaces(2);
df.format(income);

Alternatively, you may want to look into a currency formatter since you are formatting currency values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top