double型別輸出,不以科學計數法方式輸出
阿新 • • 發佈:2019-02-07
在Java語言中,當直接輸出一個double型別的資料時,預設是按科學計數法輸出,此時如果想與其他資料進行比較的時候,就比較麻煩了。
因此,如果能手動設定輸出的模式,就比較方便了。有兩種方式,
1、格式化輸出,程式碼如下:
public class TestDouble2String { public static void main(String[] args) { Double double1 = 123456789.123456789; DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");// 格式化設定 System.out.println("格式輸出:" + decimalFormat.format(double1)); System.out.println("預設輸出:" + double1); } }
2、借用BigDecimal類進行輸出,程式碼如下:
public class TestDouble2String {
public static void main(String[] args) {
double d = 123456789.123456789;
System.out.println("預設輸出:" + d);
System.out.println("格式輸出:" + BigDecimal.valueOf(d));
}
}
如此即可。