gpt4 book ai didi

java - 格式化返回字符串

转载 作者:行者123 更新时间:2023-12-01 09:19:05 24 4
gpt4 key购买 nike

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

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");

关于java - 格式化返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40287019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com