下面是雙返回類型函數,我想得到精度為 2 個零的答案。我目前的答案是-4.5. 而我希望它是-4.50。任何形式的幫助將不勝感激public double getAmount(){ double ftotal = car.hello(); return Math.round(ftotal * 100)/100.0d;}
2 回答

寶慕林4294392
TA貢獻2021條經驗 獲得超8個贊
返回的值是一個 Double,所以你不能將 0 填充到一個 Double 中。
因此,您必須格式化返回值以創建字符串,例如:
NumberFormat formatter = new DecimalFormat("#0.00");
String number = formatter.format(getTotal());
System.out.println(number);

幕布斯7119047
TA貢獻1794條經驗 獲得超8個贊
請參閱以下代碼 -
import java.text.DecimalFormat;
public class Decimal2{
private static DecimalFormat df2 = new DecimalFormat(".##");
public static void main(String[] args) {
double input = 62.123454;
System.out.println("double : " + df2.format(input));
}
}
添加回答
舉報
0/150
提交
取消