如何使用Java打印没有科学符号的双精度值?
我想用Java打印一个没有指数形式的双值如何使用Java打印没有科学符号的双精度值?,java,double,Java,Double,我想用Java打印一个没有指数形式的双值 double dexp = 12345678; System.out.println("dexp: "+dexp); 它显示了E符号:1.2345678E7 我希望它像这样打印:12345678 防止这种情况的最佳方法是什么?您可以使用printf()和%f: double dexp = 12345678; System.out.printf("dexp: %f\n", dexp); 这将打印dexp:12345678.00000
double dexp = 12345678;
System.out.println("dexp: "+dexp);
它显示了E符号:1.2345678E7
我希望它像这样打印:12345678
防止这种情况的最佳方法是什么?您可以使用printf()
和%f
:
double dexp = 12345678;
System.out.printf("dexp: %f\n", dexp);
这将打印dexp:12345678.000000
。如果不需要小数部分,请使用
System.out.printf("dexp: %.0f\n", dexp);
%中的0。0f
表示小数部分中的0个位置,即无小数部分。如果要打印小数位数为所需小数位数的小数部分,则只需提供如下数字,而不是0%.8f
。默认情况下,小数部分最多打印6位小数
这将使用中解释的格式说明符语言
原始代码中使用的默认toString()
格式已详细说明。您可以将printf()
与%f
一起使用:
double dexp = 12345678;
System.out.printf("dexp: %f\n", dexp);
这将打印dexp:12345678.000000
。如果不需要小数部分,请使用
System.out.printf("dexp: %.0f\n", dexp);
%中的0。0f
表示小数部分中的0个位置,即无小数部分。如果要打印小数位数为所需小数位数的小数部分,则只需提供如下数字,而不是0%.8f
。默认情况下,小数部分最多打印6位小数
这将使用中解释的格式说明符语言
原始代码中使用的默认
toString()
格式已详细说明。您可以使用DecimalFormat
进行尝试。使用该类,您可以非常灵活地解析数字。您可以精确设置要使用的图案。
以您的情况为例:
double test = 12345678;
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
System.out.println(df.format(test)); //12345678
您可以使用
DecimalFormat
进行尝试。使用该类,您可以非常灵活地解析数字。您可以精确设置要使用的图案。
以您的情况为例:
double test = 12345678;
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
System.out.println(df.format(test)); //12345678
只要您的数字是一个整数,这将起作用:
double dnexp = 12345678;
System.out.println("dexp: " + (long)dexp);
如果双精度变量的精度在小数点后,它将截断它。只要您的数字是整数,这将起作用:
double dnexp = 12345678;
System.out.println("dexp: " + (long)dexp);
如果双精度变量的精度在小数点之后,它将截断它。Java防止双精度表示法:
将双精度数字转换为普通数字的五种不同方法:
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:如果您不明白为什么这些随机数字出现在双精度值中超过某个阈值,本视频将解释:computerphile为什么0.1
+0.2
等于0.300000000000001
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:如果您不明白为什么这些随机数字出现在双精度值中超过某个阈值,本视频将解释:computerphile为什么0.1
+0.2
等于0.300000000000001
简而言之: 如果要消除尾随零和区域设置问题,则应使用:
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()
或System.out.println
如果Double小于10^-3或大于或等于10^7,则使用科学符号FloatingDecimal.toJavaFormatString
- 通过使用
,默认的十进制精度为6,否则可以硬编码,但如果小数较少,则会增加额外的零。例如:%f
double myValue = 0.00000021d; String.format("%.12f", myvalue); // Output: 0.000000210000
- 使用
或setMaximumFractionDigits(0)
可以删除任何十进制精度,这对于整数/长整数很好,但对于双精度:%.0f
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
- 通过使用DecimalFormat,您是本地依赖的。在法语区域设置中,小数点分隔符是逗号,而不是点:
使用英语区域设置可以确保在程序运行的任何位置都获得小数点分隔符double myValue = 0.00000021d; DecimalFormat df = new DecimalFormat("0"); df.setMaximumFractionDigits(340); System.out.println(df.format(myvalue)); // Output: 0,00000021
setMaximumFractionDigits
原因有二:
接受整数,但其实现允许的最大位数为setMaximumFractionDigits
,等于340DecimalFormat.DOUBLE\u FRACTION\u digits
因此,对于340位数字,您肯定不会舍入Double并失去精度Double.MIN_VALUE=4.9E-324
- 简而言之:
如果要消除尾随零和区域设置问题,则应使用:
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()
或System.out.println
如果Double小于10^-3或大于或等于10^7,则使用科学符号FloatingDecimal.toJavaFormatString
- 通过使用
,默认的十进制精度为6,否则可以硬编码,但如果小数较少,则会增加额外的零。例如:%f
double myValue = 0.00000021d; String.format("%.12f", myvalue); // Output: 0.000000210000
- 使用
或setMaximumFractionDigits(0)
可以删除任何十进制精度,这对于整数/长整数很好,但对于双精度:%.0f
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
- 通过使用DecimalFormat,您是本地依赖的。在法语区域设置中,小数点分隔符是逗号,而不是点:
使用英语区域设置可以确保在程序运行的任何位置都获得小数点分隔符double myValue = 0.00000021d; DecimalFormat df = new DecimalFormat("0"); df.setMaximumFractionDigits(340); System.out.println(df.format(myvalue)); // Output: 0,00000021
setMaximumFractionDigits
原因有二:
接受整数,但其实现允许的最大位数为setMaximumFractionDigits
,等于340DecimalFormat.DOUBLE\u FRACTION\u digits
因此,对于340位数字,您肯定不会舍入Double并失去精度Double.MIN_VALUE=4.9E-324
- 以下公司
{
"contact_phone":"800220-3333",
"servicer_id":"7515904334",
"servicer_name":"SOME CORPORATION"
}
BigInteger.valueOf(Long.parseLong(value, 10))
String numSring = String.format ("%.0f", firstNumber);
System.out.println(numString);