下面是双返回类型函数,我想得到精度为 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
提交
取消