0

I am developing an income tax scheme, where the calculation is based on -filing status and -taxable income

 The tax is calculated based on the following scheme:

All parts must be with exactly two digits following the decimal point.

The problem is that the output displays two zeros after the decimal places instead of the expected numbers.

For example:

Expected Value> Single Filing: $42806.50

Actual Value> Single Filing: $42806.00

Here is my code so far:

// Single Status 
        if (status == 1) {
            
            if (income > 0 && income <= 8350) {
                
                double firstTax = (int)(income * (0.10));
                double totalTax = firstTax;
                
                result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) +  ")";
            }
            
            else if (income >= 8350 && income <= 33950) {
                
                double firstTax = (int)(8350 * (0.10));
                double secondTax = (int)( (income - 8350)  * (0.15));
                double totalTax = firstTax + secondTax;
                
                result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) +  ")";
            }
            
            else if (income >= 33950) {
                double firstTax = (int)(8350 * (0.10));
                double secondTax = (int)( (33950 - 8350)  * (0.15));
                double thirdTax = (int)((income - 33950) * (0.25)); 
                
                double totalTax = firstTax + secondTax + thirdTax;
                
                result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ", Part III: $" + String.format(("%.2f"),thirdTax) + ")";
            }
            
        }

  • 3
    General Tipp: **Never** use primitive data types like `float` or `double` to do currency calculations. Those data types have limited precision, cannot represent all decimal numbers because of that and will therefor in the long run lead to calculation errors. See: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) - I would strongly recommend using a non primitive data type like `BigDecimal` if that code is ever supposed to do calculations on real money from people. – OH GOD SPIDERS Jan 26 '21 at 16:50

3 Answers3

1

You are casting them to int. This will get rid of any decimals. Remove the (int) and it should have the correct decimals.

Wouter
  • 190
  • 2
  • 8
0

// Single Status if (status == 1) {

        if (income > 0 && income <= 8350) {
            
            double firstTax = (int)(income * (0.10));
            double totalTax = firstTax;
            
            result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) +  ")";
        }
        
        else if (income >= 8350 && income <= 33950) {
            
            double firstTax = (int)(8350 * (0.10));
            double secondTax = (int)( (income - 8350)  * (0.15));
            double totalTax = firstTax + secondTax;
            
            result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) +  ")";
        }
        
        else if (income >= 33950) {
            double firstTax = (int)(8350 * (0.10));
            double secondTax = (int)( (33950 - 8350)  * (0.15));
            double thirdTax = (int)((income - 33950) * (0.25));
            
            result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ", Part III: $" + String.format(("%.2f"),thirdTax) + ")";
        }
        
    }
sahar
  • 1
0

You are type casting to int but you want your result to be double. Remove "(int)" from code. You can change code like that,

double firstTax = (int)(income * (0.10)) => double firstTax = (income * (0.10))
tugcedinc
  • 11
  • 2