1

I am to convert a double value to a String.

double d=12345678.999;
String str = String.valueOf(d);

or, just plain

String str = ""+d;

Does it, however in the exponent form-- returning the String value:

1.2345678999E7

I need the value as is in the String:

12345678.999

Is there a way to do this? I can parse the string for the value of exponent-- the portion after 'E' and go from there, but there must be an easier way of doing this.

//===========================

EDIT:

the length of the decimal part in the input isn't known, it can be any number of digits:

12345678.999

or

12345678.9999999

Imposing a format for the decimal part would fix the number of decimal digits(?)

user3880721
  • 613
  • 6
  • 16

2 Answers2

3

You should use NumberFormat to format Strings so that they do not show up in scientific notation.

String str = NumberFormat.getInstance().format(d);
user2097804
  • 1,122
  • 4
  • 13
  • 22
2

use String.format():

double d=12345678.999;
System.out.println(String.format("%12.3f", d));
morgano
  • 17,210
  • 10
  • 45
  • 56