我正在尝试格式化此返回字符串以将等级显示在小数点后一位。
return String.format(title + "," + genre + "," + releaseYear + "(" + (getRating() == -1 ? "No ratings" : getRating()) + "): " + numOfDeaths + " deaths");
我不断收到错误消息,说传递的参数太少。
最佳答案
您需要java.text.DecimalFormat。
DecimalFormat df = new DecimalFormat("0.0##");
String result = df.format(getRating());
return String.format(title + "," + genre + "," + releaseYear + "(" + (getRating() == -1 ? "No ratings" : result) + "): " + numOfDeaths + " deaths");