2

Just like in subject: i've got double like this:

2.52E-5

what i need is this:

0.0000252

and my only idea how to do it is make sth like this:

public Double change(Double d){
    String do = Double.toString(d);
    String[] a = do.split("E");
    double b = Double.parseDouble(a[0]);
    double c = Double.parseDouble(a[1]);
    Double result = (Double)Math.pow(b, c);
    return result;
}

But I'm just curious if there is already a ready method

HpTerm
  • 8,151
  • 12
  • 51
  • 67
anat
  • 53
  • 6

2 Answers2

1

You can use printf() like:

System.out.printf("%f\n", value);

Or DecimalFormat look this:

DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(8);
System.out.println(df.format(value));

Or very simply with BigDecimal and toPlainString:

new BigDecimal(value).toPlainString()
JoGe
  • 872
  • 10
  • 26
  • It returns an error : `Exception in thread "AWT-EventQueue-0" java.util.IllegalFormatConversionException: f != java.lang.String` – anat Jul 30 '15 at 07:08
  • on which line? What version of Java are you using? – JoGe Jul 30 '15 at 07:12
  • Ok, nevermind... I've forgot that this is String and it contains some letters, it works great, thanks – anat Jul 30 '15 at 07:14
0

They are the same number already. For example, on Java Repl you can do

java> 0.0000252 == 2.52e-5
java.lang.Boolean res0 = true
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154