可能重複:
Round a double to 2 significant figures after decimal point四捨五入雙Java中 - 最少小數位數
下面的代碼工作
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args){
double x = roundTwoDecimals(35.0000);
System.out.println(x);
}
public static double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.00");
twoDForm.setMinimumFractionDigits(2);
return Double.valueOf(twoDForm.format(d));
}
}
它導致35.0。 如何強制最小小數位數? 我想要的輸出是35.00
尾隨零的數量不是'double'的財產,這是一個'String'表示的屬性。 – 2012-03-19 20:14:17
http://stackoverflow.com/a/7593617/446885 – Shahzeb 2012-03-19 20:15:17
如何做到這一點而不轉換爲字符串? – 2012-03-19 20:15:45