In: Computer Science
This method will return a double value that equals to the balance + 10% of balance
How can I get a 10 percent of a balance in java?
public class TenPercent {
//Since, x % of a value = value* (x /100)
//So, 10 % of a value = value* (10 /100)
//that is equal to value* 0.1
// this function assumes the balance value is
double
// it takes the balance and adds 10% of balance to it
as follows
public static double addTenPercent(double balance)
{
return balance+balance*0.1;
}
// this function does the same assuming balance is
integer
// (double) is used to cast int value.. this converts
integer value to double
public static double addTenPercent(int balance)
{
return
(double)balance+(double)balance*0.1;
}
public static void main(String[] args) {
// TODO Auto-generated method
stub
int balance1=1000;
double balance2 =1000;
System.out.println("10% added to
the balance is ->" +addTenPercent(balance1));
System.out.println("10% added to
the balance is ->" +addTenPercent(balance2));
}
}
========================================================
Above code contains two functions, one assumes balance to be integer and the other double.
Basic idea remains the same.
Percentage of a value is given as ..
Let's say x % of y = y * ( x/100)
So, 10% of y = y * (10/100)
= y*0.1
Below is the output of the above code..
thanks