我想在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
预防这种情况的最好方法是什么?
当前回答
转换科学符号的好方法
String.valueOf (YourDoubleValue.longValue ())
其他回答
使用字符串。格式(“%。0 f”,数量)
%。0f表示0小数
String numSring = String.format ("%.0f", firstNumber);
System.out.println(numString);
这不仅适用于整数:
double dexp = 12345678.12345678;
BigDecimal bigDecimal = new BigDecimal(Double.toString(dexp));
System.out.println("dexp: "+ bigDecimal.toPlainString());
这可能是一个切线....但如果你需要把一个数值作为一个整数(太大而不能成为一个整数)放入一个序列化器(JSON等),那么你可能需要“biginteger”
例子:
值为字符串- 7515904334
我们需要在Json消息中以数字形式表示它:
{
"contact_phone":"800220-3333",
"servicer_id":7515904334,
"servicer_name":"SOME CORPORATION"
}
我们不能打印它,否则会得到这个:
{
"contact_phone":"800220-3333",
"servicer_id":"7515904334",
"servicer_name":"SOME CORPORATION"
}
像这样向节点添加值会产生期望的结果:
BigInteger.valueOf(Long.parseLong(value, 10))
我不确定这是否真的切题,但由于这个问题是我在寻找解决方案时最热门的问题,我想我应该在这里分享一下,以造福于那些搜索不好的人。: D
在我的生产代码中,当我使用它作为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
简而言之:
如果你想摆脱尾随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位数字,您肯定不会四舍五入的双精度和损失精度。