2012-08-30 53 views
0

有沒有使用指數精度進行打印的方法? 我需要填寫了微小的數字的一些數據和比較後那些....指數精度[打印雙倍,微小數字]

我有這樣的:

(0) - Mesured estimatedTime for multithread in block: 1.4917951E16 s 
(1) - Mesured estimatedTime for multithread in block: 7.531891E15 s 
(2) - Mesured estimatedTime for multithread in block: 2.9287E13 s 
(3) - Mesured estimatedTime for multithread in block: 3.28478435E17 s 
(4) - Mesured estimatedTime for multithread in block: 6.038E12 s 

,我想打印這樣的:

(0) - Mesured estimatedTime for original in block: 0000.15595175E15 s   
(1) - Mesured estimatedTime for original in block: 0007.335638E15 s    
(2) - Mesured estimatedTime for original in block: 0416.66E15 s     
(3) - Mesured estimatedTime for original in block: 0000.0390156852E15 s   
(4) - Mesured estimatedTime for original in block: 6642.0E15 s 

我知道你可以這樣強制:

// Force minimum number of digits to left and right of decimal point 
formatter = new DecimalFormat("0.0E0"); 
s = formatter.format(-1234.567);   // -1.2E3 

但不知道如何我可以強迫它Exp o-Precision:((((( help !!) :/

回答

3

看來你想暗示精度爲1e15。

for (double d : new double[]{1.4917951E16, 7.531891E15, 2.9287E13, 3.28478435E17, 6.038E12}) 
    System.out.printf("%11fE15%n", d/1e15); 

打印

14.917951E15 
    7.531891E15 
    0.029287E15 
328.478435E15 
    0.006038E15 

如果範圍更廣,您可以使用的DecimalFormat

DecimalFormat df = new DecimalFormat(); 
df.setMaximumFractionDigits(300); 
df.setGroupingUsed(false); 
for(double d = 1 ; d <= 1e30; d *= 1e5) { 
    System.out.println(df.format(d/1e15)+"E15"); 
} 

打印

0.000000000000001E15 
0.0000000001E15 
0.00001E15 
1E15 
100000E15 
10000000000E15 
+0

非常感謝! :-)整個早上都在找這個! :** s2 –

+0

printf是最簡單的格式,但可能不是您想要的。例如,我懷疑'1'會變成'1e-15e15';)希望你明白了,可以使用DecimalFormat或其他來滿足你的需求。 –

+0

Peter Lawrey你知道如果使用DecimalFormat的任何方式嗎?我沒有找到任何這些proupose ...該printf解決我的問題,但我不是最好的解決方案..對不對? –