![](/img/trans.png)
[英]How do I format a double into a string (with only two decimal places)?
[英]How would a I format currency data to two decimal places?
"袋子里有" + numItems + " 件物品。这些物品的零售成本是$" + getSubTotal() + "。这些物品的总成本,包括税,是$" + totalCost(numItems, finalTotal) + "。 ";
这是toString方法的返回语句。
我是否必须在打印行语句中添加它?
不要自己构建货币字符串 - 使用区域设置:
String output = DecimalFormat.getCurrencyInstance(Locale.GERMANY).format( 123.45);
结果:123,45 €
String.format("%.2f",<your_total>)
将返回您的 double 到 2 个小数位的字符串表示形式。
由于您正在连接字符串,因此您可以使用格式化这些方法的结果值
String.format("%.2f", someFloatValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.