How do I print a double value without scientific notation using Java?

前端 未结 14 832
一整个雨季
一整个雨季 2020-11-21 11:52

I want to print a double value in Java without exponential form.

double dexp = 12345678;
System.out.println(\"dexp: \"+dexp);

It shows this

14条回答
  •  旧巷少年郎
    2020-11-21 12:05

    The following code detects if the provided number is presented in scientific notation. If so it is represented in normal presentation with a maximum of '25' digits.

     static String convertFromScientificNotation(double number) {
        // Check if in scientific notation
        if (String.valueOf(number).toLowerCase().contains("e")) {
            System.out.println("The scientific notation number'"
                    + number
                    + "' detected, it will be converted to normal representation with 25 maximum fraction digits.");
            NumberFormat formatter = new DecimalFormat();
            formatter.setMaximumFractionDigits(25);
            return formatter.format(number);
        } else
            return String.valueOf(number);
    }
    

提交回复
热议问题