# Thread: [SOLVED] Noobie - Calculating Percentages

1. Member
Join Date
Mar 2009
Posts
62
Rep Power
0

## [SOLVED] Noobie - Calculating Percentages

Dear All,

I am trying to write a method that interprets an argument (int tax) as a percentage, and then debits an account balance by that percentage of the balance and then return the amount debited. I have no experience of calculating percentages :-(

The code so far is:

public double tax(int tax)
{
double percentage;
percentage = (tax)/100;
{
this.setBalance(this.getBalance() ..... at this point I am lost.

Do I this.getBalance * percentage? and how do I show the amount debited as the answer.

Any help would be gratefully appeciated.

All the best - FMJ.

2. you need to return the percentage before you can use it. After that you can do this.setBalance(this.getBalance() * (tax + 1))

3. --REMOVED--
reason: misunderstood the op.
Last edited by angryboy; 05-22-2009 at 04:17 PM.

4. First off when you do
Java Code:
`percentage = tax / 100;`
The number is going to be truncated before it's assigned to percentage. Make sure tax is casted to a double or the constant is declared as a floating point primitive.

Then just use simple math.

Since you are debiting the account you can simply subtract the balance by the tax percentage to get the new balance.
Java Code:
`balance -= balance*percentage`

Mr. Beans
Last edited by Mr.Beans; 05-21-2009 at 11:25 PM. Reason: Wrong operand

5. Did you try this?
Java Code:
`this.getBalance() * percentage`
Why is there an extra open bracket ({)?

Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

6. Oh, and don't use floating-point arithmetic for financial applications. Use a fixed-point system for exact and correct results.

7. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
Hey All,

So far I have:

public double tax(int tax)
{
double percentage;
percentage = (tax)/100;
this.setBalance(this.getBalance() * percentage);
}

This does not work? I am unable to change int tax as this method uses an interface. Should it then be:

public double tax(int tax)
{
float percentage;
percentage = (tax)/100;
this.setBalance(this.getBalance() * percentage);
}

I have tried this and it still does not work. Also, I'm unsure on how to show the balance that has been deducted. The only methods I have are setBalance and getBalance.

Cheers - FMJ.

PS - I am very very bad at Java, so it this seems really obvious please.. I appologise.

8. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
Further to this...?

If I simply change the method to the following:

public double tax(int tax)
{
double percentage;
percentage = (tax)*100;
return percentage;
}

then run account.tax(2);

I get 200 - This is correct 2 x 100.

But if I change back to:

public double tax(int tax)
{
double percentage;
percentage = (tax)/100;
return percentage;
}

account.tax(2); I get 0.0? Not 0.02?

What can I be doing wrong?

Cheers - FMJ.

9. Cast the tax to a double, and divide by a double, to give a double.
Java Code:
`(double)tax / 100.0;`
Using integers performs integer division, which ignores all fractional parts.

Again, floating point arithmetic will give incorrect results when dealing with very small numbers and should not be used for financial applications.

10. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
Hey OrangeDog,

Your probably gonna scream! I'm sorry I'm not quite getting this but:

public double tax(int tax); method header.

If I change to:

public double tax(double tax);

I get the error "account is not abstract and does not override abstract method tax(int) in taxable.

This class implements an interface so I cannot change it.

How can I convert int tax into a double within the method body?

I have tried:

public double tax(int tax);
{
double percentage;
double tax1;
tax1= tax;
percentage = tax1/100;
}

Does not work.

(double) tax / 100.0;

I get the error message: not a statement.

Please please ... I know that I am stupid with Java, but I don't think I am that far away.

Cheers - FMJ. ..... (aka the penny has "almost" dropped)...

11. AAAAAAAAAAAAAAAAAAAAAAAAAA - there you go :p

That is indeed not a statement, try adding "percentage = " to the front of it. The bracketed type operation is called a cast.

12. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
OrangeDog! Most excellent scream! Hehe

Thanks everyone for their help! I'm going for a beer :-)

Cheers - FMJ.

13. hey fmj, i think its about time you learn to use code tags. [noparse];)

14. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
PS - How do I mark the threads as solved?

15. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
Hey angryboy,

Heheh! Agree totally! I will use the tags from now on! :-)

Cheers - FMJ.

16. Senior Member
Join Date
Mar 2009
Posts
552
Rep Power
6
Code tags: [code] [/code] -- USE THEM

17. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
Java Code:
```message understood :-)

Thanks again all! :-)```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•