2015-01-26 93 views
1

我必須將數字四捨五入到小數點後第三位並顯示數字123.12(3)。我需要一些幫助在小數點後第三位得到括號。這裏是我的代碼:在四捨五入的小數點後面加上括號

DecimalFormat reviewRound = new DecimalFormat("##0.000"); 
+0

如果你這樣做會發生什麼:'DecimalFormat reviewRound = new DecimalFormat(「## 0.00(0)」);'? – imtheman 2015-01-26 22:48:10

+0

它會返回0.000()括號中沒有任何內容。 – 2015-01-26 22:50:54

+0

好吧,試試'DecimalFormat reviewRound = new DecimalFormat(「## 0.00'('0')'」);'。從這個答案:http://stackoverflow.com/a/3540889/845632 – imtheman 2015-01-26 22:58:40

回答

1

我不認爲這有可能只是DecimalFormat。這是我創建的一個功能,可以讓你得到你想要的。

public static String format(double d) 
{ 
    DecimalFormat test = new DecimalFormat("##0.000"); 
    String str = test.format(d); 
    String str2 = str.substring(str.length() - 1); 
    str = str.substring(0, str.length() - 1); 

    return str + '(' + str2 + ')'; 
}