## moyphus

Hi To All,

I have an equation in string like this

((BASIC+HRA)*(30/100))
Using replace function
Now i replaced this into values...
String formula = ((10000+3000)*(30/100))

whole thing as a string now 'Formula'...

How can i calculate this formula

float amt = formula;

Now i have an error...type casting....i can't calculate this amt

## puneetkay 40

Hi To All,

I have an equation in string like this

((BASIC+HRA)*(30/100))
Using replace function
Now i replaced this into values...
String formula = ((10000+3000)*(30/100))

whole thing as a string now 'Formula'...

How can i calculate this formula

float amt = formula;

Now i have an error...type casting....i can't calculate this amt

float amt = Float.parseFloat(formula);

and btw why are you storing equation result in string ?

better way :

float amt = ((10000+3000)*(30/100));

Regard,

## moyphus

float amt = Float.parseFloat(formula);

and btw why are you storing equation result in string ?

better way :

float amt = ((10000+3000)*(30/100));

Regard,

Hi Thanks For The Reply...

i already used that,But its not worked.....If There any other solution to solve this,.,.,

## moyphus

Hi,.,

Actually Before that i have like this ,.,((BASIC+HRA)*(30/100))
This is coming from database as a whole equation...
I'm replacing the Strings into values based on that BASIC And HRA,,,.

So it is String type,...i need to calculate that based on that formula,.,.,

## puneetkay 40

Hi Thanks For The Reply...

i already used that,But its not worked.....If There any other solution to solve this,.,.,

double amt = ((10000.0+3000.0)*(30.0/100.0));
System.out.println(amt);

Working fine.

Output : 3900.0

if still not working, Please post the code related to the problem.

## moyphus

Hi,,Tanx,.,

Here my code,

``````formula = compList.getPrformula();  // Here i'm getting value Like ((BASIC+HRA)*(30/100))
derFrom = compList.getDerivedFrom(); //Here, BASIC,HRA
derFrom1 = derFrom.split(",");
for (int i=0;i<derFrom1.length;i++){
CharSequence ch = "+";
CharSequence ch1 = "-";
CharSequence ch2 = "*";
CharSequence ch3 = "/";
String[] drFrom=null;
if (derFrom1[i].contains(ch))
drFrom = derFrom1[i].split("\\+");
if (derFrom1[i].contains(ch1))
drFrom = derFrom1[i].split("\\-");
if (derFrom1[i].contains(ch2))
drFrom = derFrom1[i].split("\\*");
if (derFrom1[i].contains(ch3))
drFrom = derFrom1[i].split("\\/");
if (!("".equals(drFrom) || drFrom == null)){
if (drFrom.length>0){
comp = new ArrayList<String>();
comp1 = new ArrayList<String>();
String[] com={};
for (int j = 0;j<this.availPayroll.size();j++){            comp.add(String.valueOf(this.availPayroll.get(j).getAmountStart()));
//Here In comp, i have 10000.00
}
for (int k=0;k<drFrom.length;k++){
for (int j=0;j<this.availPayroll.size();j++){
if (drFrom[k].equalsIgnoreCase(String.valueOf(this.availPayroll.get(j).getPrcompId())))
formula = formula.replaceAll(drFrom[k], this.comp.get(j));
here i'm replacing 10000.00 instead of BASIC
4000.00 instead of HRA
}
}
}
}
else {
comp = new ArrayList<String>();
for (int j = 0;j<this.availPayroll.size();j++){
}
for (int j=0;j<this.availPayroll.size();j++){
if (derFrom1[i].equalsIgnoreCase(String.valueOf(this.availPayroll.get(j).getPrcompId())))
formula = formula.replaceAll(derFrom1[i], this.comp.get(j));
here i'm replacing 10000.00 instead of BASIC
4000.00 instead of HRA
}
}

// Now ,formula = ((10000.00+4000.00)*(30/100))

double amout = Double.valueOf(formula);
``````

It Produces an error like "For Input String"

``formula = "((10000.00+4000.00)*(30/100))";``
``double amout = Double.valueOf(formula);``