自动将数字自动格式化为输入字段中的货币

时间:2019-07-08 13:31:36

标签: reactjs number-formatting intl

我有一个输入字段,如果输入数字,则应根据地区将其格式化为货币。但是我面临的问题是关于阿拉伯联合酋长国(阿拉伯联合酋长国)的区域设置。当传递给Intl.numberformat库时,我无法解析。它仅格式化最近输入的数字。请帮助我。

_getFormattedCurrency(text) {
        let formattedValue;
       if (text.includes(this.decimalSeparator)) {

           if (!this.isDecimalPresent) {                this.isDecimalPresent = true;
               // regex to Find a non-digit character globally and replace with ''
               formattedValue = text.replace(/\D/g, '');
                return formattedValue !== "" ?
                    this.currencyPrefix + new Intl.NumberFormat(this.locale).format(formattedValue) + this.decimalSeparator
                    : this.currencyPrefix;
          }
            if (text.match(this.regexForDecimalSeparator).length > 1) {
                return text.substring(0, text.length - 1).replace(/[a-zA-Z]/g, '');
            }
           // remove group separator after decimal along with any alphabets
           if(text.substring(text.indexOf(this.decimalSeparator)+1).includes(',')){
              return text.substring(0, text.length - 1).replace(/[a-zA-Z]/g, '');
            }
            return text.replace(/[a-zA-Z]/g, '');
        }

        this.isDecimalPresent = false;
        formattedValue = text.replace(/\D/g, '');
       return formattedValue !== "" ? this.currencyPrefix + new Intl.NumberFormat(this.locale).format(formattedValue)
              : this.currencyPrefix;
    }

上面的代码为我提供了针对语言环境的格式化货币。但是当我将区域设置用作带有“ AED”货币(即阿布扎比)的“ ar-AE”时,如果传递的数字是英语和阿拉伯语的混合,它将无法解析

0 个答案:

没有答案