954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need To Solve The Equation

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

moyphus
Newbie Poster
11 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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,

puneetkay
Junior Poster
122 posts since Nov 2007
Reputation Points: 51
Solved Threads: 24
 

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
Newbie Poster
11 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

moyphus
Newbie Poster
11 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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.

puneetkay
Junior Poster
122 posts since Nov 2007
Reputation Points: 51
Solved Threads: 24
 

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;i0){
comp = new ArrayList();
comp1 = new ArrayList();
String[] com={};
for (int j = 0;j();
for (int j = 0;j

moyphus
Newbie Poster
11 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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 (+, -, *, /)

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You