posted 12 years ago
hi friends i hav fallowing class.
public class TestAction {
public static void main(String[] args) {
float val=0.4f;
int newval=3;
double finval = val1 * Math.pow(10.0,newval);
System.out.println("finval1:"+finval1);
}
}
on execution finalval is 4.000000059604645E4
But i require exact double value. here it is 4.0E4.
can anyone help me
thanks alot
public class TestAction {
public static void main(String[] args) {
float val=0.4f;
int newval=3;
double finval = val1 * Math.pow(10.0,newval);
System.out.println("finval1:"+finval1);
}
}
on execution finalval is 4.000000059604645E4
But i require exact double value. here it is 4.0E4.
can anyone help me
thanks alot
posted 12 years ago
This is not an advanced question. It's pretty basic.
Floatingpoint (float or double) cannot represent every possible real number exactly. It is therefore impossible to obtain total accuracy in floatingpoint calculations.
If what you want is just to achieve a textual representation that doesn't have the random garbage in the leastsignificant digits, then you need to convert your floatingpoint values to text in a controlled way using something like the java.text.DecimalFormat class.
Floatingpoint (float or double) cannot represent every possible real number exactly. It is therefore impossible to obtain total accuracy in floatingpoint calculations.
If what you want is just to achieve a textual representation that doesn't have the random garbage in the leastsignificant digits, then you need to convert your floatingpoint values to text in a controlled way using something like the java.text.DecimalFormat class.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
posted 12 years ago
Float and double NEVER represent an exact number in the mathematical sense, so what you ask is impossilbe. However, I think you mean that you want to display the number in a certain format. you should look at NumberFormat and its subclasses for methods that allow you to format numeric output.
Layne
Layne
A feeble attempt to tell you about our stuff that makes us money
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
