Change DecimalFormat locale

前端 未结 6 1249
面向向阳花
面向向阳花 2020-12-14 07:02

I have custom DecimalFormat in Edittext\'s addTextChangedListener method, everything is working perfectly but when I change language (locale) my addTextChanged

6条回答
  •  无人及你
    2020-12-14 07:46

    You can specify the number of fraction digit after the decimal point, and/or the numbers before the decimal point. Of-course, setting the current locale is also important.

       private String formatNumber(double number) {
    
        NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
        if (nf instanceof DecimalFormat) {
            try {
                DecimalFormat formatter = (DecimalFormat) nf;
                formatter.setDecimalSeparatorAlwaysShown(true);
                formatter.setMinimumFractionDigits(2);
                formatter.setMaximumFractionDigits(2);
                return formatter.format(new BigDecimal(number);
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
            }
        }
        return null;
    }
    

提交回复
热议问题