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

问题描述 投票:0回答:1
// 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。为什么不用尼泊尔语?

android locale number-formatting
1个回答
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)}`; 
  }
 }}/>
© www.soinside.com 2019 - 2024. All rights reserved.