0

I have this code

    double d = 1234567890;
    String str = Double.toString(d);
    System.out.println(str);
    str = String.valueOf(d);
    System.out.println(str);

But the result in console is 1.23456789E9. Тhis happens when the number has more than 7 characters. Why this happens and how to make the string equal to "123456789"

  • 3
    Does this answer your question? [How do I print a double value without scientific notation using Java?](https://stackoverflow.com/questions/16098046/how-do-i-print-a-double-value-without-scientific-notation-using-java) – akuzminykh Aug 11 '20 at 14:31
  • 1
    Add another line and see what's happening: `System.out.println(new BigDecimal(d).toPlainString());`... – deHaar Aug 11 '20 at 14:33
  • Double and float are binary formats, use BigDecimal when the Decimal representation matters (Currency, display, huge count of digits ...) . See the binary representation of double here : https://stackoverflow.com/questions/322749/retain-precision-with-double-in-java – pdem Aug 11 '20 at 14:44
  • @deHaar but when i change double variable with double d = 0.123456789. This is the result 0.12345678899999999733605449137030518613755702972412109375 – Svetoslav Panteleev Aug 11 '20 at 16:49

0 Answers0