我想在Java中打印一个没有指数形式的双值。
double dexp = 12345678;
System.out.println("dexp: "+dexp);
它显示了这个E符号:1.2345678E7。
我希望它像这样打印:12345678
预防这种情况的最好方法是什么?
我想在Java中打印一个没有指数形式的双值。
double dexp = 12345678;
System.out.println("dexp: "+dexp);
它显示了这个E符号:1.2345678E7。
我希望它像这样打印: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);
}
其他回答
你可以尝试使用DecimalFormat。使用这个类,您可以非常灵活地解析数字。 您可以精确地设置您想要使用的模式。 以你为例:
double test = 12345678;
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
System.out.println(df.format(test)); //12345678
我认为每个人的想法都是正确的,但并非所有的答案都是直截了当的。 我认为这是一段非常有用的代码。下面是一些可行的方法:
System.out.println(String.format("%.8f", EnterYourDoubleVariableHere));
”。“8”是您设置要显示的小数点后数位的位置。
我正在使用Eclipse,它工作没有问题。
希望这对你有帮助。我将感激任何反馈!
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() )
}
简而言之:
如果你想摆脱尾随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位数字,您肯定不会四舍五入的双精度和损失精度。
这对我很有用。输出将是一个字符串。
String.format("%.12f", myvalue);