5

How do I convert a double value with 10 digits for e.g 9.01236789E9 into a string 9012367890 without terminating any of its digits ?

I tried 9.01236789E9 * Math.pow(10,9) but the result is still double "9.01236789E18"

Mohsin
  • 854
  • 7
  • 27
  • 1
    There might be people who take offense to being called geeks. – G_H Nov 01 '11 at 10:34
  • You probably want to cast to a long. See http://stackoverflow.com/questions/321549/double-to-long-conversion for an explaination – Alex M Nov 01 '11 at 10:38

5 Answers5

9
    double d = 9.01236789E9;    
    System.out.println(BigDecimal.valueOf(d).toPlainString());
Prince John Wesley
  • 60,780
  • 12
  • 83
  • 92
8

While 10 digits should be preservable with no problems, if you're interested in the actual digits used, you should probably be using BigDecimal instead.

If you really want to format a double without using scientific notation, you should be able to just use NumberFormat to do that or (as of Java 6) the simple string formatting APIs:

import java.text.*;

public class Test
{
    public static void main(String[] args)
    {
        double value = 9.01236789E9;
        String text = String.format("%.0f", value);
        System.out.println(text); // 9012367890

        NumberFormat format = NumberFormat.getNumberInstance();
        format.setMaximumFractionDigits(0);
        format.setGroupingUsed(false);
        System.out.println(format.format(value)); // 9012367890
    }
}
Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
0

Try String.format("%20.0f", 9.01236789E9)

Note though it's never an exact value, so "preserving every digit" doesn't really make sense.

alf
  • 8,267
  • 23
  • 45
0

You can use it.

String doubleString = Double.toString(inValue)

inValue -----> Described by you.to what position you want to Change double to a string.

SilentBomb
  • 168
  • 2
  • 11
0

In this case, you can also do

double value = 9.01236789E9;
System.out.println((long) value); // prints 9012367890
Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106