如何格式化双点?

Shi*_*n-O 45 java string format double

如何String.format使用整数和小数部分之间的点将Double格式化为String?

String s = String.format("%.2f", price);
Run Code Online (Sandbox Code Playgroud)

以上格式仅使用逗号:",".

sfu*_*ger 105

String.format(String, Object ...)正在使用您的JVM的默认语言环境.您可以使用String.format(Locale, String, Object ...)java.util.Formatter直接使用任何区域设置.

String s = String.format(Locale.US, "%.2f", price);
Run Code Online (Sandbox Code Playgroud)

要么

String s = new Formatter(Locale.US).format("%.2f", price);
Run Code Online (Sandbox Code Playgroud)

要么

// do this at application startup, e.g. in your main() method
Locale.setDefault(Locale.US);

// now you can use String.format(..) as you did before
String s = String.format("%.2f", price);
Run Code Online (Sandbox Code Playgroud)

要么

// set locale using system properties at JVM startup
java -Duser.language=en -Duser.region=US ...
Run Code Online (Sandbox Code Playgroud)

  • 添加到列表中:(new DecimalFormat(“ 0。##”,new DecimalFormatSymbols(Locale.US)))。format(price); (2认同)