%0.2f
是不正確的。您應該使用%.2f
:
例子:
System.out.printf("Age Depreciation Amount: %.2f\n", ageDepreciationAmount);
或者,如果ageDepreciationAmount
是String
做
System.out.printf("Age Depreciation Amount: %.2f\n", Double.parseDouble(ageDepreciationAmount));
順便說一句,我們通常的printf後添加\n
,而不是之前。
輸出:
Age Depreciation Amount: 10500.00
如果你想以填補空間的輸出,你可以使用%66.2
,其中66
是總寬度,2
是小數位數。但是,這隻適用於數字。
double ageDepreciationAmount = 10500.000000000002;
double ageDepreciationAmount2 = 100500.000000000002;
String tmp = String.format("$%.2f", ageDepreciationAmount);
String tmp2 = String.format("$%.2f", ageDepreciationAmount2);
System.out.printf("Age Depreciation Amount: %20s\n", tmp);
System.out.printf("Age Depreciation Amount: %20s\n", tmp2);
輸出:
Age Depreciation Amount: $10500.00
Age Depreciation Amount: $100500.00
*我是一個完整的災難,當涉及到編程*的xD – Maroun 2013-03-23 16:07:34