# [SOLVED] Noobie - Calculating Percentages

• 05-21-2009, 09:44 PM
fullmetaljacket
[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.
• 05-21-2009, 09:59 PM
xcallmejudasx
you need to return the percentage before you can use it. After that you can do this.setBalance(this.getBalance() * (tax + 1))
• 05-21-2009, 10:18 PM
angryboy
--REMOVED--
reason: misunderstood the op.
• 05-21-2009, 10:19 PM
Mr.Beans
First off when you do
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.
Code:

`balance -= balance*percentage`

Mr. Beans
• 05-21-2009, 10:21 PM
OrangeDog
Did you try this?
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)
• 05-21-2009, 10:22 PM
OrangeDog
Oh, and don't use floating-point arithmetic for financial applications. Use a fixed-point system for exact and correct results.
• 05-21-2009, 10:33 PM
fullmetaljacket
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.
• 05-21-2009, 11:03 PM
fullmetaljacket
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.
• 05-21-2009, 11:06 PM
OrangeDog
Cast the tax to a double, and divide by a double, to give a double.
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.
• 05-21-2009, 11:34 PM
fullmetaljacket
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)...
• 05-21-2009, 11:48 PM
OrangeDog
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.
• 05-21-2009, 11:54 PM
fullmetaljacket
OrangeDog! Most excellent scream! Hehe

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

Cheers - FMJ.
• 05-21-2009, 11:56 PM
angryboy
hey fmj, i think its about time you learn to use code tags. [noparse];)
• 05-21-2009, 11:57 PM
fullmetaljacket
PS - How do I mark the threads as solved?
• 05-21-2009, 11:58 PM
fullmetaljacket
Hey angryboy,

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

Cheers - FMJ.
• 05-22-2009, 01:01 AM
Singing Boyo
``` message understood :-) Thanks again all! :-)```