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

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,

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,.,.,

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,.,.,

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.

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++){
                    comp.add(String.valueOf(this.availPayroll.get(j).getAmountStart()));
                }
                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"

Is formula a String? :

formula = "((10000.00+4000.00)*(30/100))";

If yes then this will not work:

double amout = Double.valueOf(formula);

The String formula has to have a numeric value. Like "2" or "-100.345".
If it has value: "((10000.00+4000.00)*(30/100))" it will not work.
You need to extract the numbers from the String formula, turn them into numbers (parseDouble() or parseFloat()) and then apply the operators (+, -, *, /)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.