gpt4 book ai didi

java - 安卓java : format numbers with different decimal separators

转载 作者:行者123 更新时间:2023-12-01 13:15:46 25 4
gpt4 key购买 nike

我目前正在使用以下方式写入数据:

 ....
for (int i = 0; i < 3; i++) {
data = data + ";" + String.format("%.8f", values.toArray()[i]);
}
...
captureFile.println( data );

但我需要根据用户的偏好而不是区域设置更改小数分隔符(应用程序是英文的,但日志分析器可以使用 Excel 法语版本...

if (decimalSeparatorDot) 
// use current format ( US ) dot
else
// String format with comma

感谢帮助

有没有一种方法可以轻松做到这一点,或者我应该将 String.format 更改为特定的格式化程序?

我尝试了 NumberFormat 如下:

 NumberFormat nf;
boolean dot = false;
if (dot) {
nf = NumberFormat.getInstance(Locale.US);
((DecimalFormat) nf).applyPattern("#0.00000000");

} else {
nf = NumberFormat.getInstance(Locale.FRENCH);
((DecimalFormat) nf).applyPattern("#0,00000000");
}
...
for (int i = 0; i < 3; i++) { data = data + ";" + nf.format(values.toArray()[i]); }

it's working fine when dot is true ( current app Locale US ..) but it's giving all values to 0,000000000 when dot is false ( french)

最佳答案

不知道这是否是最好的方法,但它现在正在使用:

    NumberFormat nf= null;
try {
if (decimaSeparator == "dot") {
nf = NumberFormat.getInstance(Locale.US);
nf.setMinimumFractionDigits(8);
nf.setMaximumFractionDigits(8);
} else { // comma
nf = NumberFormat.getInstance(Locale.FRENCH);
nf.setMinimumFractionDigits(8);
nf.setMaximumFractionDigits(8);
}
}
catch (ClassCastException e) {
System.err.println(e);
}

关于java - 安卓java : format numbers with different decimal separators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22487972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com