Java 格式化浮点值时,语言会更改
我用的是Java12。当我尝试将浮点值的格式设置为2.006到2.01时,结果语言会发生变化。我的代码:Java 格式化浮点值时,语言会更改,java,string-formatting,Java,String Formatting,我用的是Java12。当我尝试将浮点值的格式设置为2.006到2.01时,结果语言会发生变化。我的代码: final DecimalFormat decimalFormat = new DecimalFormat("#.##"); float twt = Float.parseFloat(decimalFormat.format(weight / 1000)); input : 2.026 output found: ২.০৩ (bangla language value) 请帮助我如何解决
final DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twt = Float.parseFloat(decimalFormat.format(weight / 1000));
input : 2.026
output found: ২.০৩ (bangla language value)
请帮助我如何解决这个问题我想下面的片段会对您有所帮助
float val = 2.025f;
System.out.println(val); // 2.025
System.out.println(val / 1000); // 0.002025
System.out.format(Locale.ENGLISH, "%f\n", val); // 2.025000
System.out.format(Locale.ENGLISH, "%f\n", val / 1000); // 0.002025
System.out.format(Locale.ENGLISH, "%.2f\n", val); // 2.03
System.out.format(Locale.ENGLISH, "%.2f\n", val / 1000); // 0.00
为什么要格式化和解析<代码>浮动行波管=重量/1000.0f您是否阅读了
DecimalFormat
文档,特别是它告诉您不要调用其构造函数的部分?将格式设置为小数点后2位