Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what to use for calculation operation, help!

Status
Not open for further replies.

papaboy2

MIS
Jan 28, 2003
52
0
0
PH
hi i'm a novice in java and i want to know how can i accurately perform arithmetic operation when i'm using a double variable? becase when i assign a value for example of 100.3 it formats to
100.30000151... somthing, how can accurately use it? thanks! need help pls.


here is my code;
String mysam;
double mydouble;
mysam = CalcNumber.getText()
mydouble = Float.parseFloat(mysam);
System.out.println(mydouble);
 
Do not think you should either use :

double d = Double.parseDouble(string)

or

float f = Float.parseFloat(string)

You are mixing your data types in your example - which I expect is why you are seeing odd results.

--------------------------------------------------
Free Database Connection Pooling Software
 
but i can't retrieve the value of the textbox and assign it in my double variable, so i used a string variable and convert it to double so that i can perform aritmetic operation, what should i do to get the value of the text field and perform aritmetic operation accurately using double variable?, thanks for replying!
 
Yes, that is fine - mu point is that when you assign the double value from the string, you are using Float.parseFloat() which returns a float, instead of Double.parseDouble() which returns a double. These two primitive data types occupy different amounts of memory - hence your weird results (possibly).

See ?

--------------------------------------------------
Free Database Connection Pooling Software
 
i tried the code below but i encounter an error, LastValue is my Double variable and StrFirst is a string variable?

StrFirst = CalcNumber.getText(); LastValue = Double.parseDouble(StrFirst);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top