jakeer ahmed

Greenhorn

Posts: 15

posted 11 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.000000059604645E-4

But i require exact double value. here it is 4.0E-4.

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.000000059604645E-4

But i require exact double value. here it is 4.0E-4.

can anyone help me

thanks alot

Peter Chase

Ranch Hand

Posts: 1970

posted 11 years ago

This is not an advanced question. It's pretty basic.

Floating-point (float or double) cannot represent every possible real number exactly. It is therefore impossible to obtain total accuracy in floating-point calculations.

If what you want is just to achieve a textual representation that doesn't have the random garbage in the least-significant digits, then you need to convert your floating-point values to text in a controlled way using something like the java.text.DecimalFormat class.

Floating-point (float or double) cannot represent every possible real number exactly. It is therefore impossible to obtain total accuracy in floating-point calculations.

If what you want is just to achieve a textual representation that doesn't have the random garbage in the least-significant digits, then you need to convert your floating-point 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.

Layne Lund

Ranch Hand

Posts: 3061

posted 11 years ago

Float and double NEVER represent an

Layne

*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