我想在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);

其他回答

你可以尝试使用DecimalFormat。使用这个类,您可以非常灵活地解析数字。 您可以精确地设置您想要使用的模式。 以你为例:

double test = 12345678;
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
System.out.println(df.format(test)); //12345678

对于用double表示的整数值,可以使用这段代码,它比其他解决方案快得多。

public static String doubleToString(final double d) {
    // check for integer, also see https://stackoverflow.com/a/9898613/868941 and
    // https://github.com/google/guava/blob/master/guava/src/com/google/common/math/DoubleMath.java
    if (isMathematicalInteger(d)) {
        return Long.toString((long)d);
    } else {
        // or use any of the solutions provided by others, this is the best
        DecimalFormat df = 
            new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
        df.setMaximumFractionDigits(340); // 340 = DecimalFormat.DOUBLE_FRACTION_DIGITS
        return df.format(d);
    }
}

// Java 8+
public static boolean isMathematicalInteger(final double d) {
    return StrictMath.rint(d) == d && Double.isFinite(d);
}

我需要将一些double转换为货币值,发现大多数解决方案都是可以的,但不适合我。

十进制格式最终是我的方式,所以这是我所做的:

   public String foo(double value) //Got here 6.743240136E7 or something..
    {
        DecimalFormat formatter;

        if(value - (int)value > 0.0)
            formatter = new DecimalFormat("0.00"); // Here you can also deal with rounding if you wish..
        else
            formatter = new DecimalFormat("0");

        return formatter.format(value);
    }

正如你所看到的,如果数字是自然的,我得到-比如说- 20000000而不是2E7(等等)-没有任何小数点。

如果是小数,就只能得到两个小数。

在我的生产代码中,当我使用它作为math.Eval()函数的字符串输入时,我遇到了同样的问题,该函数接受“x + 20 / 50”这样的字符串

我看了几百篇文章……最后我选择了这个,因为它的速度。因为Eval函数最终会将其转换回自己的数字格式,而math.Eval()不支持其他方法返回的尾随E-07,而且任何超过5 dp的内容对我的应用程序来说都太详细了。

这现在被用于有1000多个用户的应用程序的生产代码中……

double value = 0.0002111d;
String s = Double.toString(((int)(value * 100000.0d))/100000.0d); // Round to 5 dp

s display as:  0.00021

Java防止E表示法出现双元:

五种将双精度数转换为正数的方法:

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Runner {
    public static void main(String[] args) {
        double myvalue = 0.00000021d;

        //Option 1 Print bare double.
        System.out.println(myvalue);

        //Option2, use decimalFormat.
        DecimalFormat df = new DecimalFormat("#");
        df.setMaximumFractionDigits(8);
        System.out.println(df.format(myvalue));

        //Option 3, use printf.
        System.out.printf("%.9f", myvalue);
        System.out.println();

        //Option 4, convert toBigDecimal and ask for toPlainString().
        System.out.print(new BigDecimal(myvalue).toPlainString());
        System.out.println();

        //Option 5, String.format 
        System.out.println(String.format("%.12f", myvalue));
    }
}

这个程序输出:

2.1E-7
.00000021
0.000000210
0.000000210000000000000001085015324114868562332958390470594167709350585
0.000000210000

都是相同的值。

Protip:如果你对为什么这些随机数字出现在double值的某个阈值之外感到困惑,这个视频解释了:为什么0.1+0.2等于0.30000000000001?

http://youtube.com/watch?v=PZRI1IfStY0