I am trying to take two numbers and get the square of them. Using several numbers some work, but this one is giving me problems pow(.0305,2). Using a calculator i get an answer of: 0.093025; but when I use java i get an answer of:9.609999999999999E-4. I need .0305 because I am taking 3.05/100 which is .0305.
I have found through trial that if I do .pow(.305,2), that does give me my need answer, but then I would have to get that with 3.05/100.
EDIT:(adding code)
double weight=3.05;
double TapeLength=100.00;
double ftwt= weight/TapeLength; this gives me: 0.093025
double ftwt= Math.pow(ftwt,2); //this gives me: 9.3025E-4
everything is cast as a double.
If you’re really getting
9.609999999999999E-4as the result, you’re doing something wrong other than what you have in you question. The following code (in Eclipse 3.7.1):produces:
which are both correct (a), just expressed in different output formats. The default for
doubleis exponential format in this case but the last line shows how you can get different formats.(a) Just on the off chance that you’re confused by the exponential form (based on your comments),
0.00093025is the same as9.3025E-4since the latter means9.3025 x 10-4(9.3025with the decimal point shifted four positions to the left.