我想在Java中打印一个没有指数形式的双值。

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

它显示了这个E符号:1.2345678E7。

我希望它像这样打印:12345678

预防这种情况的最好方法是什么?


当前回答

使用字符串。格式(“%。0 f”,数量)

%。0f表示0小数

String numSring = String.format ("%.0f", firstNumber);
System.out.println(numString);

其他回答

下面的代码检测所提供的数字是否以科学计数法表示。如果是这样,则在正常表示中使用最多“25”位数表示。

 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);
}

我的解决方案: 字符串str =字符串。格式(“%。0 f”,yourDouble);

简而言之:

如果你想摆脱尾随0和Locale问题,那么你应该使用:

double myValue = 0.00000021d;

DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
df.setMaximumFractionDigits(340); // 340 = DecimalFormat.DOUBLE_FRACTION_DIGITS

System.out.println(df.format(myValue)); // Output: 0.00000021

解释:

为什么其他答案不适合我:

Double.toString() or System.out.println or FloatingDecimal.toJavaFormatString uses scientific notations if double is less than 10^-3 or greater than or equal to 10^7 By using %f, the default decimal precision is 6, otherwise you can hardcode it, but it results in extra zeros added if you have fewer decimals. Example: double myValue = 0.00000021d; String.format("%.12f", myvalue); // Output: 0.000000210000 By using setMaximumFractionDigits(0); or %.0f you remove any decimal precision, which is fine for integers/longs, but not for double: double myValue = 0.00000021d; System.out.println(String.format("%.0f", myvalue)); // Output: 0 DecimalFormat df = new DecimalFormat("0"); System.out.println(df.format(myValue)); // Output: 0 By using DecimalFormat, you are local dependent. In French locale, the decimal separator is a comma, not a point: double myValue = 0.00000021d; DecimalFormat df = new DecimalFormat("0"); df.setMaximumFractionDigits(340); System.out.println(df.format(myvalue)); // Output: 0,00000021 Using the ENGLISH locale makes sure you get a point for decimal separator, wherever your program will run.

为什么使用340然后setMaximumFractionDigits?

两个原因:

setMaximumFractionDigits接受一个整数,但是它的实现有DecimalFormat允许的最大数字。DOUBLE_FRACTION_DIGITS等于340 翻倍。MIN_VALUE = 4.9E-324,因此使用340位数字,您肯定不会四舍五入的双精度和损失精度。

我得到了另一个涉及BigDecimal的toPlainString()的解决方案,但这次使用了string构造函数,这在javadoc中是推荐的:

此构造函数与Float返回的值兼容。toString和Double.toString。这通常是将浮点数或双精度浮点数转换为BigDecimal的首选方法,因为它不会受到BigDecimal(double)构造函数的不可预测性的影响。

它的最短形式是这样的:

return new BigDecimal(myDouble.toString()).stripTrailingZeros().toPlainString();

NaN和无限值必须额外检查,所以它的完整形式如下:

public static String doubleToString(Double d) {
    if (d == null)
        return null;
    if (d.isNaN() || d.isInfinite())
        return d.toString();

    return new BigDecimal(d.toString()).stripTrailingZeros().toPlainString();
}

这也可以复制/粘贴,以很好地与Float一起工作。

对于Java 7及以下版本,对于任何零值double,这将导致“0.0”,因此您需要添加:

if (d.doubleValue() == 0)
    return "0";

这对我很有用。输出将是一个字符串。

String.format("%.12f", myvalue);