0

I want to read a double input (say, from Scanner) and print it as is on the console. My double input can vary in precision and scale.

public class SampleTests {

    public static void main(String[] args) {
        double d = 235345345345.234534;
        System.out.printf("%f",d);
    }
}

Output:

235345345345.234530

Expected:

235345345345.234534
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Aravind Datta
  • 327
  • 4
  • 6
  • 17
  • 2
    Your expectations are not reasonable. The double type has finite precision *and* 235345345345.234534 cannot be exactly represented. Have a look at the old-but-still-good [Is floating point math broken?](https://stackoverflow.com/q/588004/238704) – President James K. Polk Mar 03 '19 at 19:57
  • Ok, I think what you said makes sense. Thank you. – Aravind Datta Mar 04 '19 at 02:41

1 Answers1

-1

If you don't know the exact scale and precision. Use %.nf where 'n' is a number that you expect the precision wouldn't be greater than.

Pablo Castro
  • 134
  • 6