0

I need my output to float to only 2 decimal places as the output is money. For example if I run:

Enter 1 if you are single. Enter 2 if you are married

1

Enter your taxable income

27060.34

Federal Income Tax:$4060.3435

I need the federal income tax to output $4060.34 instead of $4060.3435

My code:

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");
        }
    }
}
Grice
  • 1,321
  • 10
  • 24
WonderphuL
  • 49
  • 7

6 Answers6

1

If you are comfortable with C style printing (which is very much what I'd recommend here) you can use System.out.printf() which is very similar to C's printf().

System.out.printf ("Federal Income Tax: $%.2f", federalTax);
Gurusharan S
  • 345
  • 1
  • 7
1

Due to the inherent uncertainty involved with floating point arithmetic, you should always use BigDecimal for mathematical operations where knowing you have the "right" answer is important (tax program would be a good example where that's important).

This question has a good example, take good note of the first answer: Using BigDecimal to work with currencies

Community
  • 1
  • 1
StormeHawke
  • 5,534
  • 5
  • 38
  • 68
1

First of all: don't use float or double to calculate with currency values. Use BigDecimal instead.

Second to your question:

To round a value use the following formula (I use double in my example but don't do this if you do not know what you're doing):

public double round(double value) {
    return (((int)(value * 10.d))/10.d);
}

This method rounds to one valid decimal place. You have to change this implementation if you want another number of decimal places.

Uwe Plonus
  • 9,235
  • 4
  • 36
  • 47
0

Use DecimalFormat:

java.text.DecimalFormat formatter = new java.text.DecimalFormat( "$###,###,###,##0.00";-$###,###,###,##0.00" );
formatter.format( 4060.3435 );
ChrisThompson
  • 1,990
  • 12
  • 17
0
System.out.println("Federal Income Tax:$" + (double) Math.round((88306.00 +(.391*(income2 - 297350.00))))) /100.0;
Matt E
  • 427
  • 1
  • 11
  • 24
0

As others have said: Either use BigDecimal or -- the better solution in most cases -- do your calculations in pennies rather than dollars. That gets rid of most of the round-off issues immediately!

keshlam
  • 7,681
  • 1
  • 15
  • 31
  • No, calculating in pennies does not help for rounding as in financial calculation even .5 pennies can be important. – Uwe Plonus Oct 06 '14 at 14:33
  • In tax calculations, fractional pennies will *NOT* be important. In fact, the US tax system is perfectly willing to let you calculate taxes in round dollars, so even including pennies is more precision than is needed. – keshlam Oct 06 '14 at 14:34
  • I told about financial calculation in generic. I know that tax calculation is more relaxed in general but if anyone searches for help on inancial calculations in the future it could help him to determine that calculating in pennies could not be the best for his use case. – Uwe Plonus Oct 06 '14 at 14:40
  • @UwePlonus: Even so, I think you'll find that most serious banking software uses fixed-point rather than floating-point calculations. They may scale it to something smaller than a cent -- but they will still calculate to a specific number of decimal places. They could use a decimal math package, but that is FAR more compute intensive. – keshlam Oct 06 '14 at 14:43