当我使用以下方法执行截断时:
label.setText(String.format("%.2f", 1.2975118));
// 1,30
Run Code Online (Sandbox Code Playgroud)
我得到逗号(,)而不是点(。),这导致我的程序崩溃,因为我需要对浮点数执行操作。
如何截断浮点数并.setText用点而不是逗号?
请注意 String.format 取决于您当前的本地配置,您可能无法使用点作为分隔符。
喜欢使用 String.format(java.util.Locale.US,"%.2f", floatValue);
语言环境独立:
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
Run Code Online (Sandbox Code Playgroud)