NumberFormat不能作为系统区域设置的千位分隔符工作吗?

时间:2018-03-26 07:47:27

标签: android locale number-formatting

// above onCreate
private DecimalFormat decimalFormat =(DecimalFormat) 
NumberFormat.getInstance();

// inside my textWatcher

String[] splitted = originalString.split("((?<=[-+x÷])|(?=[-+.x÷]))");

                StringBuilder formatted = new StringBuilder();

                for (String word : splitted) {
                    try {
                        BigInteger num = new BigInteger(word);
                        formatted.append(decimalFormat.format(num));

                    } catch (NumberFormatException e) {
                        formatted.append(word);
                    }
                }

我把一个数字作为一个字符串变量,当我使用尼泊尔语时,我希望用2345678这样的千分隔符显示为23,45,678。严重的代码就像2345678的2,345,678一样。

令人惊讶的是,这适用于印地语作为系统语言环境,可以显示23,45,678。为什么不用尼泊尔语?

1 个答案:

答案 0 :(得分:0)

试试这段代码:

<NumberFormat thousandSeperator="." decimalSeperator="," onKeyDown = {(e) => 
{
const {key, target} = e;
const {selectionStart, value} = target;
if (key === '.') {
 e.preventDefault();
 target.value = `${value.substr(0, 
 selectionStart)},${value.substr(selectionStart, value.length)}`; 
  }
 }}/>