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

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

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

我希望它像这样打印:12345678

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


当前回答

转换科学符号的好方法

String.valueOf (YourDoubleValue.longValue ())

其他回答

只要你的号码是整数,这个方法就有效:

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

如果双精度变量在小数点后有精度,它将截断它。

Java/Kotlin编译器将任何大于9999999(大于或等于1000万)的值转换为科学计数法。Epsilion符号。

示例:12345678被转换为1.2345678E7

使用此代码避免自动转换为科学计数法:

fun setTotalSalesValue(String total) {
        var valueWithoutEpsilon = total.toBigDecimal()
        /* Set the converted value to your android text view using setText() function */
        salesTextView.setText( valueWithoutEpsilon.toPlainString() )
    }

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

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

简而言之:

如果你想摆脱尾随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位数字,您肯定不会四舍五入的双精度和损失精度。

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