我使用String.format('$%(,.2f', -125.433)将字符串输出为$(125.43)。
String.format('X', -125.43) 中的 X 应该是什么,才能输出为 -$125.43 即。前面加一个减号?
或者,我怎样才能做到这一点?
最佳答案
或者你可以使用java.text.DecimalFormat
DecimalFormat format = new DecimalFormat("$#,###0.000;$-#,###0.000");
String formatted = format.format(-125.433);
System.out.println(formatted); // prints $-125.433
或者您可以坚持使用@biplavo 在下面的评论中提到的字符串格式。
String.format("$%,.3f", -125.433)
关于java - java或groovy中的字符串格式负数减号(无括号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28082163/