格式化返回字符串

时间:2016-10-27 14:17:16

标签: java

我正在尝试格式化此返回字符串以在小数点后显示评级。

return String.format(title + "," + genre + "," + releaseYear + "(" + (getRating() == -1 ? "No ratings" : getRating()) + "): " + numOfDeaths + " deaths");

我一直收到错误,说过的参数太少了。

1 个答案:

答案 0 :(得分:1)

你想要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");